2013-04-14 67 views
4

我有一個視圖控制器,當它與完成dissming,我發佈一個notfication,並在其中包含在另一個視圖控制器子視圖,增加了作爲oberserve。但是,當它試圖執行後notificaiton梅索德,EXEC_BAD_ACCESS happend。怎麼了?該代碼是:NSNotificationCenter postNotificationName exec_badaccess

BrandListByIdViewController.m 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    NSNumber *bid = self.brands[indexPath.row][@"id"]; 
    [self dismissViewControllerAnimated:YES completion:^{ 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"SelectedBrandId" object:nil]; 
    }]; 

} 

SearchNewProduct.h 

@interface SearchNewProduct : UIView 

@end 

SearchNewProduct.m 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSelectedBrandId::) name:@"SelectedBrandId" object:nil]; 
    } 
} 

- (void)didSelectedBrandId:(NSNotification *)notif{ 

    NSLog(@"%s", __PRETTY_FUNCTION__); 
} 

即使我擺脫了用戶信息,還不能進入的。我在另一個新項目中創建了類似的情況,它工作正常。

回答

7

我沒有意識到你正在處理一個UIView而不是一個UIViewController(應該更好地閱讀你的問題)。我認爲發生的事情是View即使在發佈後也會收到通知。請確保調用deallocUIView,並刪除自身作爲觀察員:

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

而且,把一個NSLogUIViewinitWithFrame方法,看它是否被調用一次以上。

這個問題是非常相似:

ios notifications to "dead" objects

+0

'bid'不應該被釋放,因爲它是用戶信息字典的成員(保留有其價值並複製其鍵) – dreamlax

+0

我試圖把postNotificationName方法駁回前完成。無論我把代碼放在哪裏。這個視圖控制器最終會解散。我試過委託,但它似乎不工作。委託方法將不會在uiview中執行。 –

+0

修改了我的答案。我以爲你在處理兩個ViewController。代表屬於VC。你可以做的委託方法,讓擁有該視圖處理它,或嘗試上述修正答案VC。 –

1

不知道這是什麼原因,但是當你把你的觀點,以通知中心,你的選擇是錯誤的:

selector:@selector(didSelectedBrandId::) 

應該只有一個冒號。整個行應該是:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSelectedBrandId:) name:@"SelectedBrandId" object:nil]; 

兩個冒號表示該方法需要兩個參數,但它只需要一個參數。

+0

兩個列是一個錯字。 –

相關問題