简体   繁体   English

如何访问其他类中的属性/方法

[英]How to access properties/methods in other classes

I have a RootViewController that has some UITabBarController properties: 我有一个RootViewController,它具有一些UITabBarController属性:

@interface RootViewController : UIViewController<LoginViewDelegate, UITabBarControllerDelegate> {
    UIBarButtonItem*    loginButton;
    NSUserDefaults*     prefs;
    UITabBarController* arTabBarController;
    UITabBarController* stvTabBarController;
    UITabBarController* stTabBarController;
    BOOL                tabBarSaved;
}

@property(nonatomic,retain) UIBarButtonItem*    loginButton;
@property(nonatomic,retain) UITabBarController* arTabBarController;
@property(nonatomic,retain) UITabBarController* stvTabBarController;
@property(nonatomic,retain) UITabBarController* stTabBarController;
@property(assign)           BOOL                tabBarSaved;

I would like to be able to access these tabbarcontrollers from another class. 我希望能够从另一个类访问这些tabbarcontrollers。

How can I use UIApplication in another class to access the UITabBarControllers and switch tabs? 如何在另一个类中使用UIApplication访问UITabBarControllers和切换选项卡?

Try implementing a protocol in the root controller and confirm to that protocol in your class. 尝试在根控制器中实现协议,并在您的类中确认该协议。 Another way is to use notifications. 另一种方法是使用通知。

Hope that helps. 希望能有所帮助。

Also check this post: Call a function from another Class - Obj C 还要检查这个帖子: 从另一个调用类的函数-的OBJÇ

If you use @synthesize in your implementation, then what you're implementing there are public sets of getters and setters. 如果你使用@synthesize在您的实现,那么你实现什么有公共套getter和setter。

Which means, if you can get a pointer to your RootViewController here, you can also do anything you want to its .arTabBarController property. 这意味着,如果您可以在此处获得指向RootViewController的指针,则还可以对它的.arTabBarController属性进行任何.arTabBarController

You can use "keyPaths" as well. 您也可以使用“ keyPaths”。 Something like: 就像是:

UITabBarController *controller = [[UIApplication sharedApplication] valueForKeyPath:@"delegate.rootViewController.arTabController"];

Refer to the Key-Value Coding Programming Guide 请参阅《 键值编码编程指南》

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM