简体   繁体   English

在UINavigationController中,按钮不适用于pushViewController

[英]In UINavigationController, button doesn't work with pushViewController

I want to make a simple navigation control application in which I have two view controllers( ViewController and ViewController2 ). 我想制作一个简单的导航控制应用程序,其中有两个视图控制器( ViewControllerViewController2 )。 I have a button in ViewController and when I press the button, I want it to go to the other view. 我在ViewController有一个按钮,当我按下按钮时,我希望它转到另一个视图。 The problem is that button doesn't work. 问题在于按钮不起作用。 I also tried the UIViewController , but didn't work in that way neither. 我也尝试了UIViewController ,但是也没有那样。

The AppDelegate.m 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;
}

The ViewController.m ViewController.m

- (IBAction)myButton:(id)sender {

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

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

Thanks in advance for your help 在此先感谢您的帮助

if you use storyboard import in your ViewController1 the ViewController2 and give a storyboard ID to it "view2": 如果在ViewController1和ViewController2中使用情节提要导入,并为其提供一个情节提要ID“ view2”:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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