0

兩次我想通過在視圖中的對象來查看B,這是工作,但是當我重複這個方法,我有一個崩潰(線程1:EXC_BREAKPOINT)。釋放呼叫與同一個對象NSNotificationCenter

我在視圖B之前初始化爲:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hotSpotMore:) name:@"HotSpotTouched" object:nil]; 
    } 
    return self; 
} 


- (void)hotSpotMore:(NSNotification *)notification { 

     self.articleVC = [[ArticlesVC alloc] init]; 
     self.articleVC=[notification.userInfo objectForKey:@"Art"]; // ERROR LINE 


    } 

在我看來,一個爲:

 NSDictionary *myDictionary=[NSDictionary dictionaryWithObject:articles forKey:@"Art"]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"HotSpotTouched" object:self userInfo:myDictionary]; 

恢復我的對象實例變量,前兩個時間,這是工作和後有一個崩潰。

輸出: ArticlesVC setArticleVC:]:消息發送到釋放的實例0x44883f10

而在我的儀器殭屍我有這個eror: 一個Objective-C消息被髮送到解分配的 'ArticlesVC' 對象(殭屍)地址:0xc2d0710。 

我的問題是方法的dealloc被調用兩次,我有一個殭屍,因爲我的「RefCt」設置爲「-1」,我不明白爲什麼這個方法被調用了兩次時間。我如何解決這個問題?

回答

1

您的viewB已經處理,但viewA發送對象到viewB,它已經不存在。添加removeObserverdealloc

- (void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 
+0

我忘了把我的這部分代碼做到這一點,我這樣做,這種方法被稱爲兩次,也有我的問題。 ..我不知道爲什麼 –

+0

@Pierre_S,'dealloc'被調用兩次?調用'init'多少次? –

+0

當它的init和dealloc的作品被調用一次(第一次),如果它不工作也有一個初始化和兩個的dealloc(三次)。在工作和不工作之間有兩個init和兩個dealloc,也許問題來自這裏。 (第二次) –

0

您的通知觀察員將被加入你打電話initWithNibName爲你的類每次。 嘗試在添加新觀察者之前刪除先前的觀察者。

您可以在無論是在

- (void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

- (void)viewdidUnload 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 
+0

viewdidUnload不會隨時調用,而且我有同樣的問題 –