iPhone SDK: How to access a ViewController nested in a TabBar from MyAppDelegate?

爷,独闯天下 提交于 2019-12-09 23:11:11

问题


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:


回答1:


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];



回答2:


Don't you need to cast your FirstViewController?

[(FirstViewController *)[tabBarController.viewControllers objectAtIndex:0] showData]


来源:https://stackoverflow.com/questions/2083351/iphone-sdk-how-to-access-a-viewcontroller-nested-in-a-tabbar-from-myappdelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!