iPhone SDK:如何从MyAppDelegate访问嵌套在TabBar中的ViewController? [英] iPhone SDK: How to access a ViewController nested in a TabBar from MyAppDelegate?

查看:100
本文介绍了iPhone SDK:如何从MyAppDelegate访问嵌套在TabBar中的ViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的FirstViewController中调用一个方法,该方法是TabBar中第一个View的文件所有者。

I'm trying to call a method in my FirstViewController that is the File Owner of the first View in a TabBar.

我试过使用这行代码,但只是得到错误 否 '-showData' 方法发现:

I've tried using this line of code but just get the error "No '-showData' method found":

[[tabBarController.viewControllers objectAtIndex:0] showData]

但是日志说
[tabBarController.viewControllers objectAtIndex:0]
是我的FirstViewController:

But the log says that [tabBarController.viewControllers objectAtIndex:0] is my FirstViewController:

推荐答案

通常你要做的是在你的app委托中声明指向的IBOutlet视图控制器,然后使用Interface Builder将该视图控制器连接到应用程序委托中的插座。所有这一切都会导致nib加载过程在nib加载时为你设置该引用,以便它可以方便地从应用程序委托中获得。

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.

因此,尝试:

MyViewControllerClass *controller = (MyViewControllerClass *)[tabBarController.viewControllers objectAtIndex:0];
[controller showData];

这篇关于iPhone SDK:如何从MyAppDelegate访问嵌套在TabBar中的ViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆