Custom view controller help - iphone

I'm a bit confused as to implementing custom view controllers. I have a view that I want to have slide down from the top of the window. The view has three buttons on it. When the button for the view to drop is tapped the view drops. And when tapped again the view slides up/goes away. I have the drop down view saved as a nib file. Would this be the best method for implementation? Or should I have the view in the main view's nib?
And could I get some direction on how I should set it up?

The usual pattern has each of the view's stored in their own XIB file and associated with their own view controller objects. You then alloc/init the new view controller and point it to its XIB and present it modally. Once its presented, its VC responds to its actions and interacts with the model and updates its own views. You can then dismiss that view controller and its views to revert back to the parent view controller.
I have noticed a pattern mentioned in SO where people alloc/init a child VC and then within their present VC they addSubview the newVC.view, but that just seems pretty unusual to me.
If you just have a subview that is being animated down to partially cover the screen, perhaps it doesn't warrant its own VC since, as I think I'm understating your usage, its actions would map to your current VC. In that case, I would either create its contents programmatically or just as another view in the XIB for your first VC and animate that down when needed.

Related

How to push ViewController and have first controller partially visible?

I have a UINavigationController based application. I want to achieve an effect of presenting (pushing) view controller while having a part of a previous controller visible. So I want to have an effect like this:
Ideally the bottom of the second image would be a part of first controller's view.
Can this be done using UINavigationController methods, and if yes, how?
It is possible, but it requires quite a bit of efford.
Off the top of my head, I'd implement a custom container view controller, that has a UINavigationController an another custom container view controller (let's call it PreviousVCContainer) as child view controllers. See Creating Custom Container View Controllers. The "inner container view controller" would provide the view on the bottom and have the previous view controller (the one with "First title") as child view controller.
The idea is to connect the PreviousVCContainer with the UINavigationController in a way that it gets notified when the navVC is pushing or popping a vc. It would then go and look for the appropriate vc to display in the bottom view and add this vc as it's childVC.
I hope that get's you started. Let me know if you need more details on some specific points.

Display whole ViewController within another ViewController's view

Im writing an application which the main view controller is a UIViewController. It has some icons in a grid and I want to dismiss (sliding down) this grid when one of the icons is clicked. This I've done already. The problem is: when the grid is dismisseed I want another View to come from the top of the screen. This view is in this same root view controller. But I want to display the content of other view controllers in this view. For example: I want this view to show a UINavigationController with a UITableView inside it, so the user can navigate through TableViews.
I'm doing this:
HorariosViewController *horarios = [[HorariosViewController alloc] init];
[vuashView addSubview:horarios.view];
HorariosViewController is a UINavigationViewController. It shows me only a blue NavigationBar and changes like self.navigationItem.title = #"Title" won't work.
Thanks!
You can show another view controller's views as subviews but their outlets and actions remain linked to their original view controller unless you write code to make new connections, so self.whatever shouldn't be expected to affect the other view controller's properties.
(Also, if HorariosViewController is a UINavigationController, it shouldn't be created as a UIViewController.)
One approach is to have the navigation controller already there, with the icon grid presented modally on top of it. (you can set the view up this way without animations, so the user doesn't see the navigation controller underneath).
Then, when it's time for the grid to go away, it can call dismissModalViewController on itself with animation.

Creating an overlay view ontop of EAGLView

I am creating a game using OpenGLES.
Game consists of a view controller and the EAGLView.
I have created another view controller that I want to handle the extra view that go ontop of the EAGLView so things like menu and options.
I have a call from the EAGLView view controller to the extra view controller that adds an IBOUTLET UIView to the appdelegates window however its not appearing. The methods being called but no view is being added.
Probably a really easy and stupid question but I cant work it out.
Thanks for any help in advance
Ok I have done it a different way.
I use a view Controller called GameViewController to load up and i add its subview to the window in the appDelegates applicationDidFinishLaunching method
Then i call a method to add another view controllers view (my open gl view) to the subview. This means I can then put other views over the top.
I don't know why i didn't do this before to be honest
Thanks for your help
Can you be more specific with this sentence:
I have a call from the EAGLView view controller to the extra view controller that adds an IBOUTLET UIView to the appdelegates window however its not appearing.
One approach is to use navigation controller that could be initalized with EAGLView controller as root. So, you can push and pop in navigation controller another view controller that handles the game menus.
Another approach is to present menu as modal view controller. This can be invoked with presentModalViewController: in current EAGLView controller.

Managing multiple UIViews from one UIViewController

I'm getting confused on view controllers and would love a straight example. Here's the preamble:
I have a UIViewController with a matching .xib.
By default IB gives me a single View in the Document window.
I can make it appear by telling my UIWindow to addSubview:controller.view and bringSubviewToFront:controller.view
Here's the questions:
Should I add another View to the ViewController in IB? Or is there a better, programmatical way?
How do I tell the ViewController to switch between the Views?
From the ViewController downward, what does the code look like to achieve this?
I'm trying things but just making a mess so I thought I'd stop and ask...
Note that every button, label, image, etc. in your main view controller is actually a view in itself, however I've interpreted your question to mean that you want to manage multiple full-screen views or "screens". Each screen should have its own view controller to manage it. So to get the terminology right, a view-controller is an object that manages a single full-screen view (or almost full screen if it's nested inside a navigation controller or tab bar controller for example) and a view is the big area managed by the view controller as well as all the sub-views (images, buttons, labels, etc.) within it (they are all UIView sub-classes). The view controller manages all of them on that screen, if you want another screen/page then you should create a new view controller to manage it.
The root view controller (the one you add to the window) can be a plain old normal view controller that you've designed in IB, however it's probably more useful if you use a navigation controller or a tab bar controller and add your designed view controller to that - then you can push additional view controllers as needed.
Another way (if you don't want navigation or tab-bar style) would be to transition to other view controllers directly in the main window using whatever transitions you like (or just replace the old one). We'll leave that for now though.
Any sub-views of your main view controller (the one you've designed in IB) will be automatically loaded from the nib file, but you can also add your own views programatically if you want (typically you would use one or the other, i.e. nibs or programatically, but you can mix and match if you want). To do it programatically, override loadView in the view controller and then call [super loadView]; then do [self.view addSubView:myOtherView]; (create the myOtherView first of course). Note that the first time .view is accessed on your view controller, it actually calls loadView to create the view, so inside loadView it's important to call [super loadView]; before trying to access self.view :D
To switch between views, using the navigation or tab bar controllers makes it very easy. So put your main view controller inside (for example) a navigation controller and put the navigation controller in the window, so you've got window->navigationController->myController. Then from an action method in your view controller (you can hook up the action methods in IB), for example when an "about" button is pressed do this:
- (void)doAbout
{
// Create the about view controller
AboutViewController* aboutVC = [AboutViewController new];
// Push the view controller onto the navigation stack
[self.navigationController pushViewController:aboutVC animated:YES];
[aboutVC release];
}
Note that the about view controller is created programatically here - if your about view is designed in IB then instead use initWithNibName:bundle: to create it.
And that's how you manage multiple screens.

How do I push a new controller via UIButton, nested in a UIScrollView?

I have my views that are part of a tabBar. Each view has a navigationController.
In one of my views I have an embeded xib component. This is a scrolling view
with UIButtons inside it.
I want to slide in another view, inside the navigationController when a person taps the button. I can get the taps, etc.
BUT, I can't figure out how to find the controlling navigationController of that page to push the new view into. Nothing seems to work that I have tried.
IS this possible?
Thanks
You need to pass a reference to the navigation controller "down" your view hierarchy. Or pass another object that has a reference to the navigation controller "down". Or get the app delegate if it has a reference to the navigation controller.