简体   繁体   中英

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:

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. 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.

Or.. You could declare a protocol and give these modal viewControllers a property of type

`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

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


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

//set stuff

}elseIf ( etc == etc){

//setOtherStuff..

}



}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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