How to access properties/methods in other classes
-
28-09-2019 - |
Question
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?
Solution
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.
OTHER TIPS
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
You can use "keyPaths" as well. Something like:
UITabBarController *controller = [[UIApplication sharedApplication] valueForKeyPath:@"delegate.rootViewController.arTabController"];
Refer to the Key-Value Coding Programming Guide