简体   繁体   English

如何设置要在UITabBarController中加载的UIViewController的属性?

[英]How can you set a property of a UIViewController that is about to load in a UITabBarController?

I'm loading up a UITabBarController via a Storyboard as follows: 我正在通过情节提要加载UITabBarController,如下所示:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"EventsAdmin" bundle:nil];
[self.navigationController pushViewController:[sb instantiateInitialViewController] animated:YES];

The initial view controller is the uitabbarviewcontroller however I'd like to set a property on each of the view controllers that is loaded. 初始视图控制器是uitabbarviewcontroller,但是我想在每个加载的视图控制器上设置一个属性。 How can I do this? 我怎样才能做到这一点?

You can achieve this the following way: 您可以通过以下方式实现:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"EventsAdmin" bundle:nil];

id vc = [sb instantiateInitialViewController];
[self.navigationController pushViewController: vc animated: YES];

if ([vc isKindOfClass: [UITabBarController class]])
{
    for (UIViewController *controller in [(UITabBarController *)vc viewControllers])
    {
       // set your property
    }
}

In the loop you should check if controller is an instance of the view controller class you want to modify the property of. 在循环中,应检查controller是否是要修改其属性的视图控制器类的实例。

Or.. You could declare a protocol and give these modal viewControllers a property of type 或者..您可以声明一个协议,并为这些模式viewControllers提供类型的属性

`IBOutlet id <myProtocol> delegate

And hook these up to the root view controller in the storyboard. 并将它们连接到情节提要中的根视图控制器。 Then the modal viewControllers can query the delegate from their viewWillAppearAnimated: Then in your rootViewController you'll have (as part of myProtocol) a 然后,模态viewController可以从其viewWillAppearAnimated中查询委托:然后在您的rootViewController中,您将拥有一个(作为myProtocol的一部分)

-(void)setMyStuffUp:(UIViewController *)modalViewcontroller{


if ([modalViewController isKindOfClass:[viewControllerOne class]]){

//set stuff

}elseIf ( etc == etc){

//setOtherStuff..

}



}

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

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