简体   繁体   中英

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 ). I have a button in ViewController and when I press the button, I want it to go to the other view. The problem is that button doesn't work. I also tried the UIViewController , but didn't work in that way neither.

The 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

- (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":

  ViewController2 *vc2 = (ViewController2 *)[self.storyboard instatiateViewControllerWithIdentifier:@"view2"];
  [self.navigationController pushViewController:vc2 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