导航栏不会更改视图

时间:2013-09-15 02:32:53

标签: ios objective-c navigation navbar

我正在使用导航栏进行应用,该导航栏将在第一个和第三个视图之间切换(第二个和第一个视图将通过标签栏切换)。

在FirstViewController.h中:

@property(strong,nonatomic) ThirdViewController *thirdViewController;

在FirstViewController的viewDidLoad方法中我做到了:

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

还有......

- (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];

}

我的AppDelegate看起来像这样:

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;

但它不起作用,当我点击按钮时,没有任何反应。任何的想法 ? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您应该将按钮的选择器保留为goToThirdView:,但是使该方法看起来像这样:

- (void)goToThirdView:(id)sender {

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

}