简体   繁体   中英

How to access properties/methods in other classes

I have a RootViewController that has some UITabBarController properties:

@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.

How can I use UIApplication in another class to access the UITabBarControllers and switch tabs?

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

If you use @synthesize in your implementation, then what you're implementing there are public sets of getters and setters.

Which means, if you can get a pointer to your RootViewController here, you can also do anything you want to its .arTabBarController property.

You can use "keyPaths" as well. Something like:

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

Refer to the Key-Value Coding Programming Guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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