如何从选项卡中的子视图获取tabController

发布于 2024-11-30 06:24:58 字数 296 浏览 2 评论 0原文

我有一个类: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵山崖 2024-12-07 06:24:58

根据您的措辞,我假设您没有对 UITabBarController 进行子类化。我建议在所有四个视图控制器上都有一个属性,例如 theTabController,它指向您的类的实例。像这样声明此属性(在子视图中):

@class myTabController;
...
@interface MySubview : UIView {
    ...
    myTabController * theTabController;
    ...
}
...
@property (nonatomic, assign) myTabController * theTabController;

然后,在每个子视图的实现中,添加一条合成语句。即使我们在子视图的标头中有一个 @class,在 .m 中导入 myTabController 的标头也是一个好主意。我使用 @class 来防止循环导入。

#import "myTabController."
...
@implementation MySubview
@synthesize theTabController;
...
@end

myTabController 中,您需要在每个子视图上设置此属性,如下所示:

subview1.theTabController = self;
subview2.theTabController = self;
...
subviewx.theTabController = self;

最后,在每个子视图中使用 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):

@class myTabController;
...
@interface MySubview : UIView {
    ...
    myTabController * theTabController;
    ...
}
...
@property (nonatomic, assign) myTabController * theTabController;

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.

#import "myTabController."
...
@implementation MySubview
@synthesize theTabController;
...
@end

From myTabController, you need to set this property on each subview like this:

subview1.theTabController = self;
subview2.theTabController = self;
...
subviewx.theTabController = self;

Finally, use the theTabController property sub inside each subview with self.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 be MyTabController.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文