2

我认为应该在这里:

-(void) viewWillDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver:self];

}

或者也许在-dealloc.

两者对我来说听起来都很奇怪,所以我不完全确定。

首先,在我的 AppDelegate 中,我正在通过 Parse 收听远程通知

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [PFPush handlePush:userInfo];

    NSString * urlToGo = [userInfo objectForKey:@"url"];
    NSLog (@"Recibo notificación con paremetro url: %@", urlToGo);


    NSNotification *note = [NSNotification
                            notificationWithName:PUSH_NOTIFICATION
                            object:self
                            userInfo:userInfo];

    [[NSNotificationCenter defaultCenter] postNotification:note];

}

在 myViewController - (void) viewDidLoad { [super viewDidLoad];

    _lastMenuSelected=menu1;

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    [center addObserverForName:PUSH_NOTIFICATION
                        object:nil
                         queue:mainQueue
                    usingBlock:^(NSNotification *note) {

                     // Save in property to load parameter in prepareForSegure
                        _urlToLoadFromPush = urlToGoReceivedFromPush;
                    [self showPush:self];

                    }];


}



- (void)showPush:(id)sender {

    PushViewController * pushViewController=(PushViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"PushId"];

    pushViewController.url  = _urlToLoadFromPush;
    UINavigationController* nVC=[[UINavigationController alloc] initWithRootViewController:pushViewController];
    [self presentViewController:nVC animated:YES completion:^{
        //[_delegate logout];
    }];


}
4

2 回答 2

6

由于您似乎在viewDidLoad方法中添加了观察者(自 iOS 6 起仅调用一次),因此您应该在dealloc方法中删除观察者。

于 2013-11-12T16:51:07.733 回答
2

不要删除 viewWillDisappear 中的观察者,因为通常我们需要在视图在堆栈中但未出现时发布通知。所以总是尝试用观察者的名字删除 -(void)dealloc 中的观察者。

于 2015-03-03T11:23:05.650 回答