Question

I have an iOS app with multiple views.

A login UIView that the user first sees, and upon entering the right credentials, he will then be taken to the main TabBarViewController, that 'manages' the rest of the views.

The login UIView is connected to the TabBarViewController with a modal Segue. I also use this Segue to pass data from the Login view to the TabBarViewController, by using properties. All is fine until now.

As mentioned, there are other views, connected to this TabBarViewController, and this tab bar only displays them.

The question is, how can I access the property of TabViewController (which the loginView sets with a certain value), in other Views ? I want to be able to access this property from multiple other views.

Thanks.

Was it helpful?

Solution 2

Something you haven't made clear here. I'm assuming that you have subclassed UITabBarController.

If all the view controllers are children of the tab bar controller, then self.tabBarController will give you access to the tab bar.

It may end up looking something like this:

if ([self.tabBarController isKindOfClass:[MyTabBarController class]]) {
    MyTabBarController *myTabBarController = (MyTabBarController *)self.tabBarController;
    // …
}

OTHER TIPS

1.Add a property to your AppDelegate, setters and getters will be implemented automatically for you

2.In your other class access the property like so: id value = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] myGreatProperty];

If you want to share the same data from different ViewControllers, not UIView I guess, then the best approach is to use a Singleton class where you can set and get your desired data/property.

If you do not know how to write Singleton, a good tutorial is here.

Put those data in your Application's Delegate, which is actually a Singleton. The other way is to pass references of your controller to the other controllers that need to access the other's data.

You can create protocol for providing data from your properties. Confirm tab controller to your protocol and implement this protocol's method. Then in all views declare property with this protocol

id <YourProtocolName> delegate

When you create view from your tab controller set tab controller as delegate for created view. After it you can call in views

[self.delegate access_method_from_protocol]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top