2014-11-16 22 views
0

我有兩個視圖。主視圖是ViewController,下一個是AddItemViewController。 ViewController有一個tableview,列出了你在添加AddItemViewController時添加的項目。 AddItemViewController上有一個按鈕,可以繼續使用ViewController。問題是,在返回ViewController期望添加一個項目時,ViewController的私有數據突然被設置爲nil。從賽格返回後,我丟失了數據和任何與我的對象交互的機會。ViewController的私有數據集在從segue返回後設爲零

這裏是是越來越設爲零

@property (strong, nonatomic) costEstimator *myCost; 
@property NSString *testString; 

我在做什麼錯誤的數據?

這裏是在AddItemViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
// Get the new view controller using [segue destinationViewController]. 
ViewController *vC = [segue destinationViewController]; 
[vC addSomething:_selectedItem withQuantity:[_quantBox.text doubleValue]]; 
} 

任何幫助,非常感謝我的prepareforsegue代碼。謝謝:)

+0

Segues(unwinds除外)總是實例化新的控制器!所以,如果你沒有放鬆,你不會「回頭」,你正在創建一個ViewController的新實例, – rdelmar

+0

謝謝!那麼在大多數應用中常見的做法是什麼?人們不會使用賽格嗎?爲什麼沒有在iOS的基礎教程的前幾章中介紹過,比如segues? –

+0

我不知道編寫教程的人爲什麼寫他們做的事。放鬆是一種延續,它只是一種不同的類型,所以你可以使用它,或者通過彈出(如果你推動)或解散(如果你已經提交)來回到代碼中。在進行任何iOS編程之前,您應該閱讀Apple的文檔。我將從「iOS視圖控制器編程指南」開始。它解釋了很多重要的概念。 – rdelmar

回答

0

你想在這種情況下彈出AddItemViewController。當你從ViewController查找到AddItemViewController時,我猜你推動了。它有效的做法是將ViewController的AddItemViewController ontop添加到內存堆棧中。通過從AddItemViewController再次「繼續」到ViewController,您將添加AddItemViewController的另一個ViewController實例ONTOP。這就是爲什麼你認爲你實際上丟失了數據的原因,你不知道。你只看到錯誤的視圖控制器。