简体   繁体   English

在Storyboard中以编程方式访问UIViewController

[英]Accessing UIViewController programmatically in Storyboard

我正在开发一个具有多个ViewController的应用程序,并且希望从几乎每个UIViewController都可以访问特定的ViewController“登录ViewController”,我知道我可以通过使用从每个控制器到LoginViewController的segue来实现此目的,确保不是最佳解决方案,实现此目的的最佳解决方案是什么?

Use this...variable vc return the viewcontroller you are looking for 使用此...变量vc返回您要查找的viewcontroller

  UIStoryboard *aStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
  YourViewController *vc = [aStoryboard instantiateViewControllerWithIdentifier:@"YourViewController"];

you can probably do something like this 你可能可以做这样的事情

static LoginViewController *instance; //place in the implementation

- (void) viewDidLoad {
     instance = self;
}

+(LoginViewController *) getInstance { //use this method to access the instance (declare in header)
    return instance;
}

then just import the header where you need to access it and you're done 然后只需将标题导入需要访问它的位置即可

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/UsingViewControllersinYourApplication/UsingViewControllersinYourApplication.html https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/UsingViewControllersinYourApplication/UsingViewControllersinYourApplication.html

Instantiating a Storyboard's View Controller Programmatically 以编程方式实例化情节提要的视图控制器

Listing 2-2 Instantiating another view controller inside the same storyboard 清单2-2实例化同一情节提要中的另一个视图控制器

- (IBAction)presentSpecialViewController:(id)sender {
    UIStoryboard *storyboard = self.storyboard;
    SpecialViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"SpecialViewController"];

    // Configure the new view controller here.

    [self presentViewController:svc animated:YES completion:nil];
}

I suggest that you should create a base view controller which will be subclassed by every controller that needs the login view controller. 我建议您应该创建一个基本视图控制器,每个需要登录视图控制器的控制器都将其子类化。 Then in your base viewController you can create a method like this: 然后在您的基本viewController中,您可以创建如下方法:

-(YourLoginViewController*)giveMeTheLoginController {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboardName" bundle:nil];
    YourLoginViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"YourLoginViewControllerIdentifier"];
    return viewController;
}

If you don't want another base view controller you can use the same method in your view controllers to obtain a new instance of the view controller from storyboard. 如果您不希望使用其他基本视图控制器,则可以在视图控制器中使用相同的方法从情节提要中获取视图控制器的新实例。

Also using the segues from each view controller is a good way, the segues are used to define your navigation. 同样,使用每个视图控制器的segues是一种好方法,segues用于定义导航。

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

相关问题 从UIViewController访问故事板 - accessing storyboard from UIViewController 在带有情节提要的项目中以编程方式创建UIViewController - Programmatically create a UIViewController in a project with a storyboard 以编程方式为自定义UIViewController设置情节提要ID? - Set storyboard ID programmatically for custom UIViewController? 在分镜脚本中添加了UIViewController,但无法在Xamarin iOS中以编程方式读取它 - Added a UIViewController to storyboard but can't read it programmatically in xamarin ios iPhone-以编程方式确定故事板上的哪个UIViewController调用了我的课程? - iphone - Programmatically determine which UIViewController on Storyboard called my class? 故事板-以UIPopoverController的形式打开UIViewController - Storyboard - Open UIViewController as UIPopoverController 没有故事板uiviewcontroller透明于其他uiviewcontroller - no storyboard uiviewcontroller transparent over other uiviewcontroller 带有UITableView和TextField故事板放置的UIViewController - UIViewController with UITableView and TextField storyboard placement 从UITabbarItem和Storyboard创建一个UIViewController - Create a UIViewController from UITabbarItem and Storyboard 使用Storyboard将UINavigationController推送到UIViewController - Pushing UINavigationController into UIViewController Using Storyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM