iPhone SDK: How to access a ViewController nested in a TabBar from MyAppDelegate?
I'm trying to call a method in my FirstViewController that is the File Owner of the first View in a TabBar.
I've tried using this line of code but just get the error "No '-showData' method found":
[[tabBarController.viewControllers objectAtIndex:0] showData]
开发者_高级运维But the log says that [tabBarController.viewControllers objectAtIndex:0] is my FirstViewController:
Normally what you would do is declare an IBOutlet in your app delegate that points to the view controller, then use Interface Builder to connect that view controller to the outlet in the app delegate. All that does is cause the nib loading process to set up that reference for you when the nib loads so that it's handy from the application delegate.
But anyway, what you're doing will probably work but you probably need to cast the pointer in the array so that the compiler knows what class you're dealing with and whether the class has the method you're trying to call.
So try:
MyViewControllerClass *controller = (MyViewControllerClass *)[tabBarController.viewControllers objectAtIndex:0];
[controller showData];
Don't you need to cast your FirstViewController?
[(FirstViewController *)[tabBarController.viewControllers objectAtIndex:0] showData]
精彩评论