简体   繁体   English

如何在情节提要中加载没有按钮的ViewController?

[英]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接口,并且不会发生此类错误:

自动创建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中,可以控制 -从场景上方的视图控制器图标到目标场景的拖动:

创建segue 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:

创建segue

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 ):

指定segue ID

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.

相关问题 如何在情节提要中加载没有按钮的视图控制器? - How to load view controller without button in storyboard? 如何将情节提要按钮连接到其他viewController中的方法 - How to connect a storyboard button to a method in a different viewController 如何在没有情节提要的情况下设置ViewController的框架 - How to set the frame of a ViewController without storyboard Swift 5. 如何在按钮触摸时呈现或显示 ViewController? 没有故事板(以编程方式) - Swift 5. How present or show ViewController on button touch? Without Storyboard (programmatically) 如何在没有情节提要的情况下加载CollectionView - How to load CollectionView without Storyboard 从didReceiveRemoteNotification加载情节提要ViewController - load storyboard viewcontroller from didReceiveRemoteNotification 从ViewController加载一个storyboard(UITableViewController) - Load an storyboard (UITableViewController) from ViewController 如何在 ios siwft 中以编程方式将 storyboard 视图控制器加载到标签栏 controller 中? - How to load storyboard viewcontroller into programmatically tabbar controller in ios siwft? 没有StoryBoard的情况下如何在两个ViewController之间使用委托? - How to use delegate between two ViewController without StoryBoard? Swift 4.2:如何在 ViewController 中嵌入 UIPageControl(没有 Storyboard)? - Swift 4.2: How to embed UIPageControl in ViewController (without Storyboard)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM