我有一个两个控制器,即aViewController
和bViewController
。我在bViewController
(称为txt1
)中有一个文本字段。我已经声明如下:
在 bViewController.m 中:
- (void)viewDidLoad
{
[self addObserver:self forKeyPath:@"txt1.text" options:NSKeyValueObservingOptionNew context:nil];
}
在 aViewController.m 中:
- (void)viewDidLoad
{
bViewController *obj = [[bViewController alloc] init];
[obj addObserver:self forKeyPath:@"txt1.text" options:NSKeyValueObservingOptionNew context:NULL];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
/*
NSLog(@"%@",keyPath);
NSLog(@"%@",object);
NSLog(@"%@",change);
*/
NSLog(@"%s",__PRETTY_FUNCTION__);
if ([keyPath isEqualToString:@"txt1.text"]) {
NSLog(@"text1 content changed");
}
}
当我在 txt1 中添加一些文本时(点击返回键后),我收到如下错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<bViewController: 0x9134560>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: txt1.text
Observed object: <bViewController: 0x9134560>
Change: {
kind = 1;
new = werwetw;
}
Context: 0x0'
*** First throw call stack:
(0x1c92012 0x10cfe7e 0x1c91deb 0xb85406 0xb2267d 0xb2233c 0xb09417 0xb22b24 0xad7d60 0xb21eb5 0xdd707 0xe4b02 0xedda1 0xdc645 0x121fb5 0x1220e1 0xdc4e6 0x2a0a 0xe4d2b 0xed9f8 0x1923cf 0x198f7f 0x198a8c 0x1979fe 0x1a1c72 0x24ddb 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x24e35 0x24806 0x24beb 0x16698 0x1beddf9 0x1bedad0 0x1c07bf5 0x1c07962 0x1c38bb6 0x1c37f44 0x1c37e1b 0x1bec7e3 0x1bec668 0x13ffc 0x1fa2 0x1ed5)
libc++abi.dylib: terminate called throwing an exception
谁能告诉我哪里出错了。