0

我有几个关于 NSNotification 及其观察者生命周期的问题。

UPD。

我将简化我的应用程序的逻辑,使其看起来很原始:

ViewController A 有按钮“评论”,还包含一个 UIView B。在这个 UIView 上,我们还有另一个按钮“分享”。如果用户登录,每个按钮都会执行它应该做的事情,如果没有,它会从 NSObject 类“Logistic”(逻辑最多的地方)调用“login”方法,并出现弹出视图 C。因此,我在 C 中创建了一个 postNotificationName,以使按钮在用户登录时收听 - 完成他们的工作。

例如在 viewController A

- (void) comment{
    if (<user_logged_in>){
    //do the magic
                [self removeObserver];
        } else {
            [self removeObserver];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(comment) name:@"dismiss_popup" object:nil];
            [Logistic login];
        }
}

我对“共享”方法(在视图 B 中)做同样的事情,但是当我按下按钮“评论”时,然后跳过 C - 登录弹出窗口,然后按下“共享”按钮,进行登录- 之后,“分享”和“评论”同时开始行动

我想我应该调用 removeObserver

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"dismiss_popup" object:nil];

但是如何在 UIView B 仍然存在的情况下做到这一点?

问候 。

4

3 回答 3

1

您可以在物流中修改您的登录方式,如下所示:

函数原型:

+ (void)loginComplete:(void(^)(BOOL success))complete;

函数本身:

+ (void)loginComplete:(void(^)(BOOL success))complete {
//login code
BOOL success = YES;//or no if it was some problems))
complete(success);//replace notification post with this
}

最后你在 viewController A 中:

- (void) comment{
if (<user_logged_in>){
//do the magic
            [self removeObserver];
    } else {
        [self removeObserver];
        //we don't need Notification center anymore
        [Logistic loginСomplete:^(BOOL success) {
            handle login completion
        }];
    }

}

于 2013-09-06T16:47:28.750 回答
0
- (IBAction)commentButton:(id)sender {
    if (!user logged in) {
        [self.logistic loginUserWithCompletion:^(id result){
            if (![result isKindOfClass:[NSError class]]) {
                [self doStuff];
            }
        }];
    } else {
        [self doStuff];
    }
于 2013-09-06T17:46:18.217 回答
0

回顾您的问题:

  • A 是 B 的父母。
  • A和B打电话给C。
  • A 和 B 听 C.
  • 当 C 发送通知时,A 和 B 都响应,但您只希望其中一个响应。

我建议让 A 处理与 C 的所有通信,因此如果 B 需要与 C 通信,则需要通过 A。这是您可以这样做的方法:

在 UiViewController A

-(void) viewDidLoad{

 //Adding a notification listener should be done in initialization stage.
 [[NSNotificationCenter defaultCenter] addObserver:self 
                                          selector:@selector(dismissViewC:) 
                                              name:@"dismiss_popup" object:nil];
}
- (void) comment{
    if (<user_logged_in>){
         //proceed with comment
    } else {           
            NSNumber* caller=[NSNumber numberWithInt:COMMENT];
            [Logistic login:caller];
    }
}
-(void) dismissViewC:(NSNotification*) notify{
  (NSNumber*) callerId =[notify object];
   switch(callerId.intValue){
      case COMMENT: //proceed with comment;
      case: SHARE: [self.viewB share];
   }
}
-(void) dealloc {
    // removing an observer should be done in the dealloc stage
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

在 UIView B 中:

 - (void) share{
        if (<user_logged_in>){
             //proceed with share
        } else {           
                NSNumber* caller=[NSNumber numberWithInt:SHARE];
                [Logistic login:caller];
        }
    }

物流需要将新的登录参数传递给 C,C 将如下所示:

-(void) login:(NSNumber*) caller{
   self.myCaller=caller;
   // proceed with login
}
-(void) dismissMe{
      [[NSNotificationCenter defaultCenter] postNotificationName:@"dismiss_popup"
                                                          object:self.caller];
}
于 2013-09-09T07:46:51.243 回答