私はApple Documentsを読んでいて、それを見ました:
通知を監視しているオブジェクトが割り当て解除される前に、通知センターに通知の送信を停止するように指示する必要があります。そうしないと、次の通知が存在しないオブジェクトに送信され、プログラムがクラッシュします。
アプリが実際にどのように機能しているかをよりよく知るために、アプリをクラッシュさせようとしました。
ただし、このコードを SecondViewController の dealloc 内に配置しなくても、通知の送信後にクラッシュすることはありません。私は明らかにオブザーバーを追加し、secondViewController から戻って、viewController に通知をプッシュしています。では、このプログラムがクラッシュしないのに、なぜオブザーバーを削除する必要があるのでしょうか?
[[NSNotificationCenter defaultCenter] removeObserver:self];
残りのコードは次のとおりです。
//ViewController:
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib. }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated. }
- (IBAction)go:(id)sender {
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self presentViewController:secondViewController animated:NO completion:^{}];
    [secondViewController release], secondViewController = nil; }
- (IBAction)push:(id)sender {
    // All instances of TestClass will be notified
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:self]; }
//SecondViewController:
@implementation SecondViewController
- (void)dealloc {
    [super dealloc]; }
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(receiveTestNotification:)
                                                     name:@"TestNotification"
                                                   object:nil];
    }
    return self; }
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
     }
- (void) receiveTestNotification:(NSNotification *) notification {
    // [notification name] should always be @"TestNotification"
    // unless you use this method for observation of other notifications
    // as well.
    NSLog (@"Successfully received the test notification!"); }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated. }
- (IBAction)back:(id)sender {
    NSLog(@"");
    [self dismissViewControllerAnimated:NO completion:^{}]; }