I am creating some sample applications to understand the concepts of view navigation, binding etc in cocoa. Here is the scenario: I have a window that has a tab view(2 tabs) in MainMenu.Xib. I have a text field in the first tab and label in the second tab. I want both of them to reflect the same value and I want to do this using binding. Also, I don't want to use the views provided to me along with the tab view.
These are the steps I have done.
The view of each tab view item is set separately in the applicationDidFinishLaunching: method using the following code:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
//initialize view controllers
view1=[[ViewTab1 alloc] initWithNibName:@"ViewTab1" bundle:nil];
view2=[[ViewTab2 alloc] initWithNibName:@"ViewTab2" bundle:nil];
//set views
[[[myTabView tabViewItems] objectAtIndex:0]setView:view1.view];
[[[myTabView tabViewItems] objectAtIndex:1]setView:view2.view];
}
- myTabView is the outlet reference of the tab view from MainMenu.xib in AppDelegate.
- ViewTab1 is the name of the first view controller (and the xib).
- ViewTab2 is the name of the second view controller (and the xib).
ViewTab1 has one single text field (and an associated label). I have bound this to a variable(name) declared in AppDelegate. ViewTab2 has a label. I have bound this also to the same variable in AppDelegate.
The variable, 'name' is initialized in the init method of AppDelegate.
AppDelegate.h
....
NSString *name;
....
@property(strong) ViewTab1 *view1;
@property(strong) ViewTab2 *view2;
@property (assign) IBOutlet NSTabView *myTabView;
@property (strong) NSString *name;
....
AppDelegate.m
....
@synthesize myTabView;
@synthesize view1,view2;
@synthesize name;
....
- (id)init {
self = [super init];
if (self) {
name=@"dummy";
}
return self;
....
Apart from this I haven't done any coding in my program.
In the ViewTab1.xib I got an object and made it an instance of AppDelegate and then connected the delegate reference of the Application object(NSApplication) to the same object. (I hope this is the right way of getting the AppDelegate object.)
I did the same in ViewTab2.xib
Then I bound the text field in ViewTab1 and label in ViewTab2 to this variable in AppDelegate.
When I run the program both the text field and label shows "dummy". But when I change the value in the text field, its not reflected in the label in the second tab( i.e. ViewTab2).
Please tell me what I'm doing wrong.