Reputation: 15582
I'm new to Objective-C, and I'm looking for some advice on how to manage multiple view controllers.
I've looked through Apple's documentation on their built-in container view controller classes, and none of them seem to be what I'm looking for -- the closest is NavigationController, but even that seems a little bit off.
I want to implement a series of ViewControllers -- which use xibs for their interfaces -- that transition from one to the next according to a series of rules. For example, on app load, we see if we have a userId
in local storage -- if we don't, show the signup screen. Next, there's a button to (say) order a taxi -- if that button is clicked, show the confirm screen.
Optional Aside: The reason I don't think this fits the Navigation controller is that the flow doesn't seem hierarchical, but rather kind of branchy and linear. One concrete example of this is that I don't need a navigation bar to go back, which seems to come standard on the Navigation Controller. But I don't know the NavigationController well enough to know for sure whether or not it fits this usecase.
I've been hacking this with a variety of methods. For example, in an IBAction handler, I've been using this code to transition to a new view controller:
UIViewController *view = [[UIViewController alloc] initWithNibName:@"CCWConfirmViewController" bundle:nil];
view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:view animated:YES completion:nil];
Also, in my window's 'Root' ViewController (which I set to be the SignupViewController) initWithNibName, I return a different view controller than the one asked for, depending on the result of the local storage call I mentioned earlier:
if (currentUser.userId) {
// Instead of returning the SignupViewController, like was asked,
// return the MainViewController, since signup isn't needed for existing
// users.
CCWMainViewController *mvc = [[CCWMainViewController alloc] init];
return mvc;
I seem like I have to be doing something wrong (the second hack builds but generates a warning, since I'm returning a pointer to the wrong type). Anyone know a better way? Is the NavigationController for me after all, and I'm just misinterpreting its purpose? Do I just need to implement a custom container to serve as my RootViewController and manage these other ViewControllers?
Upvotes: 0
Views: 102
Reputation: 1500
Your decision is right. You'll not need a navigation controller for your purpose, but as they say.. There are a lot of ways by which you can achieve a result.
"I don't need a navigation bar to go back, which seems to come standard on the Navigation Controller"
You can always hide the navigation bar using self.navigationController.navigationBarHidden = YES
Coming back to the point, I would not say what you have done is wrong but would propose a better approach which involves the concept of view containment.
In cocoa touch you can add any view controller as a child view controller. So here's what I propose.
Create a class called RootViewController which will always be created and set to your window regardless of the condition the user is logged in or not. In the viewDidLoad
of this class
-(void)viewDidLoad
{
if (currentUser.userId) {
CCWMainViewController *mvc = [[CCWMainViewController alloc] init];
[self addChildViewController:mvc];
mvc.view.frame = self.view.bounds;
[self.view addSubview:mvc.view];
}
else{
//Create signup/login view and add to view as above.
}
}
Upvotes: 1