I am new to iPhone. I am trying to implement a KVO mechanism.
What I have?
two TabController with two UIViewController, FirstViewController has a button, SecondViewController has a UITextView
What I want?
When a button is clicked in the first control, it updates the member variable that secondViewController should observe, and it must be added to the UITextView.
What I've done?
FirstViewController.h
@interface FirstViewController : UIViewController { IBOutlet UIButton *submitButton; } -(IBAction)submitButtonPressed; @property (retain) NSString* logString; @end
FirstViewController.m
-(IBAction)submitButtonPressed { NSLog (@" Submit Button PRessed "); self.logString = @"... BUtton PRessed and passing this information "; }
SecondViewController.h
@interface SecondViewController : UIViewController { IBOutlet UITextView *logView; } @property (nonatomic, strong) UITextView *logView; @end
SecondViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { ...... NSArray *vControllers = [self.tabBarController viewControllers]; FirstViewController *fvc = [vControllers objectAtIndex:0]; [fvc addObserver:self forKeyPath:@"logString" options:NSKeyValueObservingOptionNew context:NULL]; return self; } -(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog (@".... OBSERVE VALUE FOR KEY PATH..."); }
What result do I expect?
The string ".... MONITORING THE VALUE FOR THE KEY TO THE PATH ..." should be printed every time I click the button in the FirstViewController.
What am I getting?
No OUtput.
What am I doing wrong? Please help me
source share