我不明白为什么当我拨打NSNotification
时,我正在尝试发送的UIButton
没有被调用。我正在尝试使用ViewController
将对象从ViewController
发送到其他Notifications
。
我的代码:
在 Viewcontroller.m :
中 - In Init method:
[self.goPanelRightButton addTarget:self action:@selector(btnMovePanelRight:) forControlEvents:UIControlEventTouchUpInside];
- (IBAction)btnMovePanelRight:(id)sender
{
UIButton *button = sender;
switch (button.tag) {
case 0: {
[self movePanelToOriginalPosition];
break;
}
case 1: {
[self sendModel];
NSLog(@"Entered case 1");
[self movePanelRight];
break;
}
default:
break;
}
}
-(void)sendModel{
[[NSNotificationCenter defaultCenter] postNotificationName:kModelNotification
object:self
userInfo:@{GALLERY_KEY:self.model}];
在 ObserverViewController :
中-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(getModelMain:)
name:kModelNotification
object:nil];
}
}
-(void)getModelMain:(NSNotification *) notification{
NSDictionary *dict = [notification userInfo];
self.model=[dict objectForKey:GALLERY_KEY];
}
谢谢;)
答案 0 :(得分:0)
我使用键值编码解决了问题。在我切换到下一个视图之前传递对象:
SEL selector = NSSelectorFromString(@"setModel:");
if ([self.leftPanelViewController respondsToSelector:selector]) {
[self.leftPanelViewController setValue:self.model forKey:@"model"];
}