Xcode 4.5 ios6按钮在objective-c中打开新视图

Pet*_*ter 2 objective-c ios

我知道如何通过拖放来获取一个按钮来在故事板中打开一个新的ViewController(新类).但我需要一个代码,我可以在.m文件中编写,在单击我的按钮时打开一个新视图.

在我的FirstViewController.mi得到:

- (IBAction)button1:(id)sender {

}
Run Code Online (Sandbox Code Playgroud)

我需要什么代码才能让按钮打开SecondViewController?

Vit*_*ata 8

[self performSegueWithIdentifier: @"TheSequeName" sender: self];
Run Code Online (Sandbox Code Playgroud)

当您按住某个按钮以在故事板上打开新的视图控制器时,将定义@"TheSequeName"

如果您不使用segue,则有许多变体可用于打开新的视图控制器.基本的是使用单独的NIB(没有故事板)

SecondViewController *view = [[SecondViewController allow] initWithNibName:@"NibName" bundle:nil];
Run Code Online (Sandbox Code Playgroud)

如果在Storyboard上声明视图控制器,则可以使用

SecondViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"viewStoryboardId"]; 
Run Code Online (Sandbox Code Playgroud)

然后使用导航控制器显示SecondViewController

[self.navigationController pushViewController:view animated:YES];
Run Code Online (Sandbox Code Playgroud)

或者作为模态视图

[self presentModalViewController:view animated:YES];
Run Code Online (Sandbox Code Playgroud)