[英]How to load viewcontroller without button in storyboard?
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? 有什么问题吗?
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
. 我知道您在询问如何在不使用按钮的情况下执行此操作,但是演示的问题是您使用了按钮,但未正确将其连接到
IBAction
。 Your goNext
should be defined in your .h as: 您的
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. 我想您是手动创建该
IBAction
。 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: 将来值得注意的是,如果您从按钮向下控制 -drag(或右键单击拖动)到助手编辑器文件,则可以自动创建
IBAction
接口,并且不会发生此类错误:
As an aside, I personally like to use a segue instead, so I first define the push segue between the view controllers themselves. 顺便说一句,我个人更喜欢使用segue,因此首先定义视图控制器自身之间的push segue。 In Xcode 6, one would control -drag from the view controller icon above the scene to the destination scene:
在Xcode 6中,可以控制 -从场景上方的视图控制器图标到目标场景的拖动:
In Xcode versions prior to 6, one would control -drag from the view controller icon in the bar below the scene: 在6之前的Xcode版本中,可以从场景下方栏中的视图控制器图标中控制 -drag:
I then select the segue in Interface Builder and give it a "storyboard identifier" (in this example, I called it pushTo10
): 然后,我在Interface Builder中选择segue,并为其指定“ storyboard标识符”(在本示例中,我将其称为
pushTo10
):
I then have my goNext
execute that segue, rather than manually invoking pushViewController
: 然后,我让
goNext
执行该命令,而不是手动调用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. 您不必以这种方式执行此操作,但这是需要注意的另一种选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.