0

I need a good explanation how can I handle the UINavigationControllers and the UITabBarControllers on iOS6.1 with StoryBoards.

  1. When I load my app (1st ViewController) I need if (FB login = success) it jumps with segues to the 2nd ViewController automatically. Here I think I can't use a UINavigationController like root, apple's HIG don't like it. image 1
  2. I need a UITabBarController that connects to 3 UICollectionViewControllers (one tab for each one). I have to put the UITabBarController like root? If yes, how can I handle the others Viewontrollers between them? Like this: Image 2
  3. I need a custom BarButtonItem (like the "Delete All" that you can see on the image 2) on each CollectionViewController, I need to use a UINavigationController for each one?
Fabio Cardoso
  • 1,181
  • 1
  • 14
  • 37
  • I checked some sites and I think I can't have a `NavigationBar`before a `TabBarController`, right? – Fabio Cardoso Mar 14 '13 at 20:00
  • It is not a good idea to have a TabBarController contained inside a NavigationController as it usually creates a confusing user experience. What relationship do the four buttons on your second viewController in image 1 have to your tab bar controller? Are [Restaurantes, Alojamento, Maps, Favorites] the same as the tab bar sections? Or do these buttons navigate somewhere else? – foundry Mar 16 '13 at 18:35
  • Only Favorites button goes to the TabBarController. – Fabio Cardoso Mar 16 '13 at 18:41

1 Answers1

3

Let's assume you are happy to use unwind segues (if not there are many ways to do without).

1 When I load my app (1st ViewController) I need if (FB login = success) it jumps with segues to the 2nd ViewController automatically. Here I think I can't use a UINavigationController like root, apple's HIG don't like it.

You 1st VC (lets call it the loginVC)..
- should NOT be contained in the Navigation Controller.
- should be set as the application's initialViewController

Your 2nd VC (lets call it your startVC)
- SHOULD be contained in the Navigation Controller
- in that Navigation Controller's Identity Inspector, assign a storyboardID: @"InitialNavController"

In your App Delegate, let's have a loggedIn BOOL property:

@property (nonatomic, assign) BOOL loggedIn;

Now, in your LogInViewController...

In viewDidAppear check to see if we are already logged in, if so, navigate immediately to your startVC:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if ([(AppDelegate*)[[UIApplication sharedApplication] delegate] loggedIn]) {
        UINavigationController* navController = 
           [[self storyboard] instantiateViewControllerWithIdentifier:@"InitialNavController"];
        [self presentViewController:navController
                           animated:NO
                         completion:nil];
    }
}

It's important that this is placed in viewDidAppear, not (for example) in viewDidLoad - the segue won't work unless the initial view is properly initialised and onscreen.

Make an unwind Segue (and declare it in loginVC's @interface) … loginVC will be the destination VC if users log out.

- (IBAction)unwindOnLogout:(UIStoryboardSegue *)segue 
{
    [(AppDelegate*)[[UIApplication sharedApplication] delegate] setLoggedIn:NO];

}

(corrected - removed this line:
[[self presentedViewController] dismissViewControllerAnimated:YES completion:nil];
we don't need to dismiss as the segue has already done that behind the scenes. This is redundant and logs an error message)

In other viewControllers, whereever appropriate you can make a 'logout' button. CTRL-drag from that button to the 'exit' symbol at the bottom of the ViewController in the storyboard, and you will be able to select this segue as the unwind segue.

2 I need a UITabBarController that connects to 3 UICollectionViewControllers (one tab for each one). I have to put the UITabBarController like root? If yes, how can I handle the others Viewontrollers between them? Like this:

I think you are trying to work out how the tabBarController relates to the NavigationController in the previous viewController (startVC). The answer is, it shouldn't - you really don't want to embed the Tab Bar VC in the previous Nav Controller as it will create weird situations for the Tab Bar's child viewControllers.

The navigation from startVC to the tabBarVC should be via a modal segue, NOT a push segue.

You can make another unwind Segue in startVC to facilitate return from your tabBarController's viewControllers:

- (IBAction)unwindToInitialNavFromModal:(UIStoryboardSegue *)segue {

}

(corrected - removed this line:   [[self presentedViewController] dismissViewControllerAnimated:YES completion:nil];   this method doesn't need any content to perform the dismissing)

3 I need a custom BarButtonItem (like the "Delete All" that you can see on the image 2) on each CollectionViewController, I need to use a UINavigationController for each one?

You won't get a Navigation bar in your tabBarVC by default.

You can provide one in two ways
- embed each child viewController in it's own Navigation Controller;
- manually drag a navigation bar to EACH child viewController's scene.

Either is fine, it really just depends whether you will want navigation to other ViewControllers.

You can then add a barButtonItem on the left or right to connect up to the initialVC's unwind segue (CTRL-drag to the 'exit' symbol).

enter image description here

foundry
  • 31,615
  • 9
  • 90
  • 125
  • I'll try to implement your ideas and then I'll say if it worked. Thank you for the explanation. – Fabio Cardoso Mar 16 '13 at 20:19
  • Can you check if you are talking about something like [this](http://tinypic.com/r/23i9oqs/6)? – Fabio Cardoso Mar 17 '13 at 15:50
  • @FabioCardoso No, I am following your storyboard. Check my update, I have added a picture. – foundry Mar 17 '13 at 17:38
  • Only one thing, and this will be perfect, on the TabBarVC with NavController how can I hide that "item 1" that appears on the NavController? And I want that "TB Item 2" appears on the tabbar. – Fabio Cardoso Mar 18 '13 at 03:44
  • @FabioCardoso, you can directly edit the text in the Navigation bar by double-clicking on it. You can directly edit the text in the tab item by double clicking on it in the view controller (for tab item 2 in this example this is the Navigation Controller). – foundry Mar 18 '13 at 03:51
  • 1
    @FabioCardoso, I found a redundant line in the unwind segues, check my updated answer. – foundry Mar 21 '13 at 03:03
  • Is it possible to unwind from UINavigationController to UITabBarController.? – Vendetta Mar 11 '14 at 07:17