如何从选项卡中的子视图获取tabController
我有一个类:myTabController,在这个类中我有一个UITabBarController,在这个UITabBarController中有4个子视图。
现在我在我的第一个子视图中,假设它是 tab1, 我可以调用 self.parentViewController 来获取 UITabBarController,而这个 UITabBarController 是 myTabController 拥有的,但是我怎样才能获取 myTabController 呢?因为我想访问 myTabController 中的一些数据。
谢谢提前, 问候
I have a Class: myTabController, in this class I have a UITabBarController, which has 4 subviews in this UITabBarController.
Now I am in my first subview, say it's tab1,
I can call self.parentViewController to get the UITabBarController, and this UITabBarController is owned by myTabController, but How can I get myTabController then? Cause I want to access some data in myTabController.
Thanks Ahead,
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的措辞,我假设您没有对 UITabBarController 进行子类化。我建议在所有四个视图控制器上都有一个属性,例如
theTabController
,它指向您的类的实例。像这样声明此属性(在子视图中):然后,在每个子视图的实现中,添加一条合成语句。即使我们在子视图的标头中有一个
@class
,在.m
中导入myTabController
的标头也是一个好主意。我使用@class
来防止循环导入。在
myTabController
中,您需要在每个子视图上设置此属性,如下所示:最后,在每个子视图中使用
theTabController
属性 sub 和self.theTabController
。我还必须指出:类名以小写字母开头永远都不好。
myTabController
实际上应该是MyTabController
。From your wording I am assuming that you have not subclassed UITabBarController. I would suggest having a property on all four view controllers, something like
theTabController
, which points to an instance of your class. Declare this property like this (in the subviews):Then, in each subview's implementation, add a synthesize statement. It's also a good idea to import the header of
myTabController
in the.m
, even though we have an@class
in the subview's header. I used an@class
to prevent circular imports.From
myTabController
, you need to set this property on each subview like this:Finally, use the
theTabController
property sub inside each subview withself.theTabController
.I also have to point out: it's never good to have a class name that starts with a lower case letter.
myTabController
should really beMyTabController
.