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 以降は 1 回だけ呼び出されます)、deallocメソッド内のオブザーバーを削除する必要があります。

于 2013-11-12T16:51:07.733 に答える
2

viewWillDisappear のオブザーバーを削除しないでください。通常、ビューがスタックにあるが表示されない場合に通知を投稿する必要があるためです。したがって、オブザーバーの名前で -(void)dealloc 内のオブザーバーを常に削除するようにしてください。

于 2015-03-03T11:23:05.650 に答える