UIButton触发时未调用NSNotification

时间:2013-12-27 14:53:38

标签: uibutton notifications nsnotificationcenter

我不明白为什么当我拨打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];
}

谢谢;)

1 个答案:

答案 0 :(得分:0)

我使用键值编码解决了问题。在我切换到下一个视图之前传递对象:

SEL selector = NSSelectorFromString(@"setModel:");

if ([self.leftPanelViewController respondsToSelector:selector]) {

   [self.leftPanelViewController setValue:self.model forKey:@"model"];

}
相关问题