Combining Navigation Controller with Tab Bar Contr

2019-02-17 12:34发布

问题:

As I mentioned in the title, I want to add Navigation Controller to my application which already has a Tab Controller. So trying to do the staff something like on this page. Anyway, something is wrong. UINavigationController is looking a blank page, even if has a view and some libraries.

Let me begin from the stracht:

In my AppDelegate, I'm setting tab bar controllers like this:

@interface MYAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBarController;

@end

And here is .m file:

@implementation MYAppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    application.applicationSupportsShakeToEdit = YES;
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UINavigationController *viewController1 = [[[MYMainViewController alloc] init] initWithNibName: @"MYMainViewController" bundle:nil];
    UIViewController *viewController2 = [[[MYPageViewController alloc] init] initWithNibName:@"MYPageViewController" bundle:nil];
    UIViewController *viewController3 = [[[MYSearchViewController alloc] init] initWithNibName:@"MYSearchViewController" bundle:nil];
    UIViewController *viewController4 = [[[MYPersonViewController alloc] init] initWithNibName:@"MYPersonViewController" bundle:nil];

    // Initialize tabBarController and add ViewControllers
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects: viewController1, viewController2, 
        viewController3, viewController4, nil];

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];

    return YES;
}

Then, here is MYMainViewController implementaion which is a UINavigationController:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@", [self navigationController]); // Logging null
}

My .xib file has a UINavigationController and and there is a view in it. Althought it, when I worked the app, there is blank page and untitled navigation bar. What am I doing wrong?

If I could see the content of my view, I want to navigate between two view controllers by using back button.

Any help or approach would be great for me.

回答1:

Try to remove navigation controller from xib, so it only have view controller, then initialize navigation controller programatically:

UIViewController *tmpViewController1 = [[[YourViewController alloc] init] initWithNibName:@"YourViewController" bundle:nil];
 UINavigationController *viewController1 =  [[UINavigationController alloc] initWithRootViewController:tmpViewController1];