简体   繁体   中英

Navigation bar not changing views

I'm doing an app with a Nav bar which will switch between the first and third views (the second and the first ones will be switched by a tab bar).

In the FirstViewController.h:

@property(strong,nonatomic) ThirdViewController *thirdViewController;

In the viewDidLoad method of the FirstViewController I made it:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Model"
style:UIBarButtonItemStylePlain target:self action:@selector(goToThirdView:)];

And also...

- (void)goToThirdView:(id)sender
{

    ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];

    self.thirdViewController = thirdViewController;   

    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

    [self.view removeFromSuperview];

    [self.view insertSubview:self.thirdViewController.view atIndex:0];

    [UIView commitAnimations];

}

My AppDelegate is looking like this:

UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;

But it's not working, when I click in the button, nothing happens. Any idea ? Thank you in advance.

You should leave the selector for the button as goToThirdView:, but make that method look like this:

- (void)goToThirdView:(id)sender {

    ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    [self.navigationController pushViewController:thirdViewController animated:YES];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM