2013-01-12 27 views
0

我在Objective C中有點弱我承認,我的最終目標是將數據從ViewController3傳遞迴ViewController1。事實上,這部分已經完成並取得成功。但是,當調用[self.navigationController popToRootViewControllerAnimated:YES]時,我得到EXC_BAD_ACCESS。使用委託時彈出ViewControllers

ViewController1 <ViewController2Delegate> 
- (void) didAddEventLocation:(Event *)event { 
    NSLog(@"Event name = %@", event.name); //Shows name successfully 
} 

ViewController2 <ViewController3Delegate> 
- (void) didAddEvent:(Event *)event { 
    [self.delegate didAddEventLocation:event]; 
} 

ViewController3 
[self.delegate didAddEvent:event]; 
[self.navigationController popToRootViewControllerAnimated:YES]; 

對不起格式錯誤的代碼,只是想簡化。做[self.navigationController popViewControllerAnimated:YES]沒有問題,但它只需要我ViewController2。我知道我在這裏做了一些非常錯誤的事情,但是我不知道如何解決它。如果我需要澄清,請告訴我。

+0

你肯定'[self.delegate didAddEventLocation:eventTmp]'是正確的,因爲參數是'event'? –

+0

是的,很抱歉..當我複製/粘貼我的代碼時發生錯字。現在編輯。 – xhermit

回答

2

使用殭屍來追捕什麼給你EXC_BAD_ACCESS。某些對象已經釋放,現在正在彈回到根視圖控制器時被調用。

嘗試此鏈接: How do I set up NSZombieEnabled in Xcode 4?

+0

@xhermit是的,這必須解決你的問題。你會看到哪個對象被分配。 – kaspartus

+0

謝謝。我之前使用過Instruments來追捕殭屍,我猜我忘了我應該在我的堆棧追蹤中尋找Event Type = Zombie。在彈出導航堆棧之前,我沒有調用[self.locationManager stopUpdatingLocation]來追蹤它。再次感謝! – xhermit

1

看起來,你的控制器之一(首先在我看來)被釋放。在VC3方法中檢查self.navigationController是否存在。那麼你必須檢查他所有的風險投資。我認爲沒有什麼能夠持有第一個VC。問題可以通過使用(例如)addChildViewController您的導航控制器的方法,或者如果smth將引用您的控制器來解決。

此外,如果您在兩個實例之間存在路徑問題,則可以使用NSNotificationCenter從一個實例向另一個實例發送一些信息。

HTH!

+0

[self.navigationController.viewControllers objectAtIndex:x]顯示viewControllers,因爲我期望他們在堆棧上。 – xhermit