如何在故事板中加载没有按钮的viewcontroller?

use*_*822 1 xcode storyboard ios

我想加载没有按钮的视图控制器.我将标识符设置为10并尝试使用

if(...){ 
//load ViewController2
UIViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"10"];
[self.navigationController pushViewController:vc];
}
Run Code Online (Sandbox Code Playgroud)

这是我的progect http://www.sendspace.com/file/kfjhd5

什么问题?

Rob*_*Rob 10

您问题中的上述代码很好.我知道你问的是如何在没有按钮的情况下做到这一点,但你演示的问题是你使用了一个按钮,但没有正确地将它连接到IBAction.您goNext应该在.h中定义为:

- (IBAction)goNext:(id)sender;
Run Code Online (Sandbox Code Playgroud)

实施应该是:

- (IBAction)goNext:(id)sender
{
    UIViewController *vc=[[self storyboard] instantiateViewControllerWithIdentifier:@"10"];
    [self.navigationController pushViewController:vc animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

我认为你是IBAction手动创建的.在将来值得注意的是,如果你control从按钮向下(或右键单击拖动)到助手编辑器文件,你可以自动创建IBAction接口,这种错误不会发生:

自动创建IBAction


顺便说一句,我个人喜欢使用segue,所以我首先定义视图控制器本身之间的推送segue.在Xcode 6中,可以control从场景上方的视图控制器图标拖拽到目标场景:

创建segue xcode 6

在6之前的Xcode版本中,可以control从场景下方的栏中的视图控制器图标中删除:

创造segue

然后我在Interface Builder中选择segue并给它一个"故事板标识符"(在这个例子中,我称之为pushTo10):

指定segue id

然后我goNext执行segue,而不是手动调用pushViewController:

- (IBAction)goNext:(id)sender
{
    [self performSegueWithIdentifier:@"pushTo10" sender:self];
}
Run Code Online (Sandbox Code Playgroud)

这样做的好处是我的故事板现在可以直观地表示我的应用程序的流程(而不是看起来有一个浮动的场景)和导航栏之类的图形元素在故事板中正确表示.

你不必这样做,但它是另一个需要注意的选择.