0

I have a UITabBar application with 4 tabs. Each tab has it's own ViewController.

Sometimes I want to access variables from Tab1ViewController in Tav2ViewControllerTab3.

For example:

Tab1ViewController.h

#import <UIKit/UIKit.h>
@interface Tab1ViewController : UIViewController {
NSMutableArray *contentArray;
}
@property (nonatomic, retain) NSMutableArray *contentArray;
@end

Tab2ViewController.m

#import "Tab2ViewController.h"
#import "Tab1ViewController.h"
@implementation Tab2ViewController
- (void) viewDidLoad {
NSLog(@"Data count Tab1: %@", [Tab1ViewController.contentArray count]);
}

This is not working, because it seems as xcode expect contentArray to be a method. But how can I access the property from another ViewController? What am I doing wrong?

Thanks, Pat

2 Answers 2

1

Do you have an instance of Tab1ViewController? You are trying to access the property on the class, not the instance of the class. This would only work if the property were static. I should also warn you that tabs that communicate with each other may indicate some lack of clarity in your app's design. Ideally, your view controllers should talk to a central place.

Sign up to request clarification or add additional context in comments.

2 Comments

So I could define some instance variables in App-Delegate which I initialize after the application start and create 4 get-methods to receive those instances from each tabcontroller? Would that be best practice?
@crimi - that certainly sounds a much more promising option. You tab view controllers can either use [[UIApplication sharedApplication] delegate] to access your app's delegate, or you could write your own delegate protocol that your app delegate implements.
1

I suggest that you try to decouple your controllers from each other. When any controller can be aware of any other controller then a change to any one controller can require a change to all other controllers. That doesn't scale and makes your life as a developer unnecessarily difficult.

Instead there are a number of effective patterns for breaking your app up into self-contained modules which can still communicate but don't need that sort of application wide tight coupling.

If you want to send events between controllers then consider the delegate pattern to allow one controller to communicate with a second, delegate, controller without requiring that delegate to be aware of the first controller. Better yet fire notifications to allow controllers to announce these events without any need to know about the other controller(s) listening for them.

If you want to share data between controllers then construct a shared model object (or service for obtaining models like a ManagedObjectContext) to store this data and give each controller a reference to it. That way your app delegate might construct this model and pass it to each controller. Each controller can observe changes to the model and make modifications to it as necessary without needing to be aware of the other controllers also using that model.

Apple's WWDC talk on MVC might be a useful guide for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.