[英]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.