0

的頂部的參考視圖我在一些UIViewControlle,需要返回到前一個,我用這個代碼:如何獲得在NavigationController

NSLog(@"%@", [self.navigationController popViewControllerAnimated:YES]); 
// Here I need to reference to the view I am going go to to call some function before this view is being displayed 
NSLog(@"Done button pressed"); 

但(寫在我需要獲得對執行彈出功能後顯示的視圖的引用(上一個viewController。

回答

1

)您可以使用UINavigationController屬性topViewController,該屬性爲您提供堆棧頂部的視圖控制器 只需在彈出之前調用它,或者將其設置爲下一個:)

您還可以訪問viewControllers屬性以獲取導航控制器堆棧包含的所有視圖控制器。

+0

兩者的回報'(空)'如果叫'popViewController前後獲得參考對象'功能! –

+0

如果您在調用poViewController _method_之前調用它,該怎麼辦? – Geoffroy

+0

它返回當前視圖控制器,但我需要一個ref到前面的viewController –

0

查看online docs中的viewControllers物業。它會給你視圖控制器的一個陣列中的導航堆棧:

根視圖控制器是在索引0處的陣列中,所述後視圖控制器是在索引n-2,並且頂控制器是在索引n-1,其中n是數組中的項目數。

所以你想要的視圖控制器將在索引n-2

0

再說什麼@Geoffroy和@Maurice凱利說..

我應該調用popViewControllerAnimated:功能

// I should get reference to the object here before calling the popViewControllerAnimated: function 
    NSLog(@"%@", [self.navigationController popViewControllerAnimated:YES]); 
    NSLog(@"Done button pressed"); 
+0

'[self.navigationController popViewControllerAnimated:YES]'返回一個你剛纔丟棄的視圖控制器的引用。這不是你在原始問題中所要求的:*「執行彈出功能後顯示的視圖」* –