ios 取消注册观察者

标签 ios objective-c

我正在阅读 Apple 文档并看到:

Before an object that is observing notifications is deallocated, it must tell the notification center to stop sending it notifications. Otherwise, the next notification gets sent to a nonexistent object and the program crashes.

我尝试让应用程序崩溃,以更好地了解它的实际工作原理。

但是,即使我没有将此代码放入 SecondViewController dealloc 中,发送通知后它仍然不会崩溃。显然我正在添加观察者并从 SecondViewController 返回并在 viewController 中推送通知。那么,如果这个程序不会崩溃,为什么我们需要删除观察者呢?

[[NSNotificationCenter defaultCenter] removeObserver:self];

其余代码是:

// View Controller :

@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]; }

//第二个 View Controller :

@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:^{}]; }

最佳答案

@Reno Jones 是对的。 像这样删除观察者 - [[NSNotificationCenter defaultCenter] removeObserver:self name:@"TestNotification"object:nil];

在他的答案中还要添加的一件事是,您应该删除 - (void)dealloc {} 方法中的观察者 - 这是释放 self 时调用的方法。

编辑:

我查看了代码,发现您没有使用 arc。还有一个问题,为什么你不在你的应用程序中使用 ARC?你有充分的理由通过引用计数来给自己施加压力吗?我不明白这一点?

其次,您可以在 viewDidLoad 方法中移动 addObserver 并查看它是否会导致您的应用程序崩溃。

关于ios 取消注册观察者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16611450/

相关文章:

如果连接不可用,iOS 服务请求的重试机制

javascript - 使用 Javascript 获取内容的高度,移动 Web View

objective-c - 使用 UITableView 向下钻取分层数据

ios - SKEmitterNode 如何在不同背景下使用混合模式 "add"保持相同效果

objective-c - 无法理解 Objective C 中的以下代码?

ios - 在 Swift SpriteKit 中将静态阴影附加到动态 physicsBody 球的底部

ios - 验证码总是无效 Swift Parse

iphone - ERROR : You are not the current participant. 当我是当前参与者时。什么?

objective-c - 如何从nsarray删除空格,方括号和“

将 NSNotificationCenter 添加到 UIView 的 iOS 异常