简体   繁体   English

iOS Xcode 4.2使用StoryBoard呈现ViewController

[英]iOS Xcode 4.2 using StoryBoard to present a ViewController

The new way of presenting a viewcontroller using the StoryBoard. 使用StoryBoard呈现视图控制器的新方法。

    UIStoryboard* secondStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

UINavigationController* secondViewController = [secondStoryboard instantiateViewControllerWithIdentifier:@"Connect"];

[self presentViewController: secondViewController animated:YES completion: NULL];

The old way of presenting the controller Connect is like this 呈现控制器Connect的旧方法是这样的

  Connect *connect = [[[Connect alloc] initWithNibName:@"Connect" bundle:nil] autorelease]; 

[self presentViewController:connect animated:YES completion:NULL];
 NSString *userid;
 userid=@"123";

 [connect setID:userid];

I want to call the setID function of the connect controller in the Storyboard way, how can I do that? 我想以情节提要方式调用连接控制器的setID函数,该怎么办? Seems like I don't get an instance of Connect controller directly. 好像我没有直接获得Connect控制器的实例。

You should subclass the view controllers so that you can control what happens when users interact with it (unless your app can function on segues alone.) 您应该对视图控制器进行子类化,以便可以控制用户与之交互时发生的事情(除非您的应用程序只能按顺序运行)。

In Xcode, File -> New -> File -> Cocoa Touch Class. 在Xcode中,依次选择“文件”->“新建”->“文件”->“可可接触类”。 Make a class like MyAwesomeViewController that subclasses (in your case) UINavigationController . 创建一个类似MyAwesomeViewController的类,该类MyAwesomeViewController UINavigationController (在您的情况下)。

I like to make a custom method called NewVC in my custom view controller classes. 我喜欢在自定义视图控制器类中创建一个名为NewVC的自定义方法。 It can do everything you list above, plus any custom setup: 它可以完成您在上面列出的所有内容,以及任何自定义设置:

+(MyAwesomeViewController *)NewVC {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @"MyStoryboard" bundle: nil];
    return [storyboard instantiateViewControllerWithIdentifier: @"MyAwesomeViewController"];
}

This way when you want to create a new one you can just call [MyAwesomeViewController NewVC] and it'll return a new view controller instance. 这样,当您要创建一个新的实例时,您只需调用[MyAwesomeViewController NewVC] ,它将返回一个新的视图控制器实例。

Why are you casting it as UINavigationController? 为什么将其转换为UINavigationController? Just do what you were doing. 随便做什么吧。

Connect* connect = [secondStoryboard instantiateViewControllerWithIdentifier:@"Connect"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM