我讀蘋果文檔,發現:IOS註銷觀察員
被觀察通知的對象被釋放之前, 必須告訴通知中心停止發送它的通知。 否則,下一個通知被髮送到一個不存在的對象,並且程序崩潰。
我試圖崩潰的應用程序,以更好地瞭解它是如何工作的。
但是,即使我沒有將此代碼放入SecondViewController dealloc中,它在發送通知後仍不會崩潰。我顯然添加了觀察者並從secondViewController返回並在viewController中推送通知。那麼,爲什麼我們需要刪除觀察者,如果這個程序沒有崩潰?
[[NSNotificationCenter defaultCenter] removeObserver:self];
休息代碼:
//視圖控制器:
@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:^{}]; }
刪除觀察者這樣 - [[NSNotificationCenter defaultCenter] removeObserver:@「TestNotification」];並通知像這樣 - [[NSNotificationCenter defaultCenter] postNotificationName:@「TestNotification」object:nil userInfo:nil]; –
確定SecondViewController在通知被觸發之前被釋放了嗎? – thegrinner
至於如果您未能移除觀察者就會崩潰,因此沒有*保證*您的應用在測試時會崩潰。但是當有人使用它時幾乎肯定會崩潰,並且很可能在Apple通過認證運行您的應用程序時。 –