xcode 如何在情节提要中加载没有按钮的视图控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12499695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load viewcontroller without button in storyboard?
提问by Rob
I want to load view controller without button. I set the identifier to 10 and tried to use
我想加载没有按钮的视图控制器。我将标识符设置为 10 并尝试使用
if(...){
//load ViewController2
UIViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"10"];
[self.navigationController pushViewController:vc];
}
here is my progect http://www.sendspace.com/file/kfjhd5
这是我的项目http://www.sendspace.com/file/kfjhd5
whats a problem?
有什么问题吗?
回答by Rob
The above code in your question is fine. I know you were asking about how to do this without a button, but the problem with your demonstration was that you used a button, but didn't properly hook it up to the IBAction
. Your goNext
should be defined in your .h as:
您问题中的上述代码很好。我知道您在询问如何在没有按钮的情况下执行此操作,但是您的演示的问题在于您使用了一个按钮,但没有正确地将它连接到IBAction
. 您goNext
应该在 .h 中定义为:
- (IBAction)goNext:(id)sender;
And the implementation should be:
实施应该是:
- (IBAction)goNext:(id)sender
{
UIViewController *vc=[[self storyboard] instantiateViewControllerWithIdentifier:@"10"];
[self.navigationController pushViewController:vc animated:YES];
}
I presume you created that IBAction
manually. In the future it's worth noting that if you control-drag (or right-click drag) from the button down to the assistant editor file, you can automate the creation of your IBAction
interfaces, and this sort of error won't happen:
我想你是IBAction
手动创建的。以后值得注意的是,如果您control从按钮向下拖动(或右键单击拖动)到助手编辑器文件,您可以自动创建您的IBAction
界面,并且不会发生这种错误:
As an aside, I personally like to use a segue instead, so I first define the push segue between the view controllers themselves. In Xcode 6, one would control-drag from the view controller icon above the scene to the destination scene:
顺便说一句,我个人更喜欢使用 segue,所以我首先定义了视图控制器本身之间的 push segue。在 Xcode 6 中,可以control从场景上方的视图控制器图标 -drag 到目标场景:
In Xcode versions prior to 6, one would control-drag from the view controller icon in the bar below the scene:
在 Xcode 6 之前的版本中,可以control从场景下方栏中的视图控制器图标中拖动:
I then select the segue in Interface Builder and give it a "storyboard identifier" (in this example, I called it pushTo10
):
然后我在 Interface Builder 中选择 segue 并给它一个“故事板标识符”(在这个例子中,我称之为pushTo10
):
I then have my goNext
execute that segue, rather than manually invoking pushViewController
:
然后我让我goNext
执行那个 segue,而不是手动调用pushViewController
:
- (IBAction)goNext:(id)sender
{
[self performSegueWithIdentifier:@"pushTo10" sender:self];
}
The advantage of that is that my storyboard now visually represents the flow of my app (rather than appearing to have a scene floating out there) and graphical elements like the navigation bar are correctly represented in the storyboard.
这样做的好处是我的故事板现在可以直观地表示我的应用程序的流程(而不是看起来有一个场景漂浮在那里),并且导航栏等图形元素在故事板中正确表示。
You don't have to do it this way, but it's another alternative to be aware of.
您不必这样做,但这是另一种需要注意的选择。