简体   繁体   中英

How to modify view controller created by storyboard?

I have created a tabbed application in Xcode 5 by using storyboard with 5 different tabs.

Now I want to access the fifth view controller to pragmatically modify some fields, so i wish to access the view controller automatically created by the storyboard.

I have given the view controller a identifier and in my viewDidLoad method, i have used following codes

UIStoryboard *mainStoryboard = [self storyboard];
ProfileViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier: @"loginviewcontroller"];
[controller setFields:PFLogInFieldsFacebook | PFLogInFieldsDefault];
[self presentViewController:controller animated:YES completion:nil];

but apparently a new view controller is created and presented on the top of original view controller instance which is created by storyboard automatically, and the tab bar navigation also disappears.

How shall access that view controller created by storyboard automatically or this is the wrong approach? If this is the wrong approach, how shall I achieve this task?

Don't present your view controller, because your view controller is already a part of your tabbar. So select it using the following code:

[self.tabBarController setSelectedIndex:yourIndex];

Your code would be like this:

UIViewController *controller = self.tabBarController.viewControllers[4];
[controller setFields:PFLogInFieldsFacebook | PFLogInFieldsDefault];
[self.tabBarController setSelectedIndex:4];

Enjoy :)

You access it through your tab bar controller. From any controller embedded in the tab bar controller you can get a reference like this:

UIViewController *controller = self.tabBarController.viewControllers[4];

To set the fields, instead of trying to set them from another controller, just pass the data that the #5 controller needs to do that itself. Since the view of #5 won't have been loaded until it's viewed the first time, you can't modify its view from another controller. So, in ProfileViewController, create and integer property, lets call it fieldValues, and pass the data to it. In the controller where you want to pass the information:

ProfileViewController *controller = self.tabBarController.viewControllers[4];
controller.fieldValues = PFLogInFieldsFacebook | PFLogInFieldsDefault;

Then in viewDiLoad of ProfileViewController, do this:

[self setFields:self.fieldValue];

从Apple文档中获取有关instantiateViewControllerWithIdentifier的信息:

You use this method to create view controller objects that you want to manipulate and present programmatically in your application. Before you can use this method to retrieve a view controller, you must explicitly tag it with an appropriate identifier string in Interface Builder.

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