在UINavigationController中,按钮不适用于pushViewController

时间:2013-07-29 14:05:29

标签: ios uinavigationcontroller pushviewcontroller

我想制作一个简单的导航控制应用程序,其中我有两个视图控制器(ViewControllerViewController2)。我在ViewController中有一个按钮,当我按下按钮时,我希望它转到另一个视图。问题是按钮不起作用。我也尝试了UIViewController,但也没有这样做。

AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    ViewController *vc1 = [[ViewController alloc]init];

    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:vc1];

    self.window.rootViewController = nav;


    [self.window makeKeyAndVisible];
    [self.window addSubview:nav.view];
    return YES;
}

ViewController.m

- (IBAction)myButton:(id)sender {

    ViewController *vc2 = [[ViewController alloc]init];

    [[self navigationController]pushViewController:vc2 animated:YES];
}

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

如果您在ViewController1中使用Storyboard导入ViewController2并为其提供故事板ID“view2”:

  ViewController2 *vc2 = (ViewController2 *)[self.storyboard instatiateViewControllerWithIdentifier:@"view2"];
  [self.navigationController pushViewController:vc2 animated:YES];