Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

ios6 - How to handle UINavigationControllers and UITabBarControllers iOS 6.1

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?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...