我是iPhone新手。我正在嘗試實施KVO機制。UITABViewController中兩個UIViewControllers之間的KVO機制
我有什麼?
2 TabController有兩個UIViewController中,FirstViewController有一個按鈕, SecondViewController有一個UITextView
我想要什麼?
當在firstViewController中按下按鈕時,它更新成員變量,應該被secondViewController觀察到,它應該附加到UITextView。
我做了什麼?
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...");
}
我期望輸出什麼?
每次按下FirstViewController中的按鈕時,都應該打印字符串「.... OBSERVE VALUE FOR KEY PATH ...」。
我得到了什麼?
否OUtput。
我做錯了什麼?請幫助我
感謝您的回覆。我對ios非常感興趣。你介意提供更多的信息,以便我可以更多地瞭解它嗎? – Whoami 2012-08-09 14:05:19
編輯答案顯示一些粗暴的代碼,讓你朝着正確的方向 – CSmith 2012-08-09 14:23:57
至於靜態'實例',它的指針總是一樣的嗎?既然是單身? – Honey 2016-04-14 12:06:18