簡體   English   中英

iOS - 如何初始化UINavigationController以顯示推送的ViewController?

[英]iOS - How can I initialize a UINavigationController to display a pushed ViewController?

我想知道如何初始化UINavigationController以顯示堆棧中的第三個視圖控制器? 有點像郵件應用程序。 即使你殺了應用程序,當你啟動它時,你會看到包含所有郵件的視圖控制器,並有“后退”按鈕,允許你進入郵箱列表。

謝謝你的回答。

您可能想要查看UINavigationControllersetViewControllers:animated:方法:

參數

viewControllers

視圖控制器放置在堆棧中。 此陣列中控制器的前后順序表示導航堆棧中控制器的新的從下到上的順序。 因此,添加到數組的最后一項成為導航堆棧的頂部項。

動畫

如果是,則為頂視圖控制器的推動或彈出設置動畫。 如果為NO,則替換視圖控制器而不使用任何動畫。

討論

您可以使用此方法更新或替換當前視圖控制器堆棧,而無需顯式推送或彈出每個控制器。 此外,此方法允許您更新控制器集,而無需動畫更改,這可能適用於您希望將導航控制器返回到先前狀態的啟動時間。

如果啟用了動畫,則此方法根據items數組中的最后一項是否已在導航堆棧中來決定執行哪種類型的轉換。 如果視圖控制器當前在堆棧中,但不是最頂層的項,則此方法使用彈出過渡; 如果它是最頂層的項目,則不執行轉換。 如果視圖控制器不在堆棧中,則此方法使用推送轉換。 僅執行一次轉換,但是當轉換完成時,堆棧的全部內容將替換為新的視圖控制器。 例如,如果控制器A,B和C位於堆棧上並且您設置了控制器D,A和B,則此方法使用彈出過渡,結果堆棧包含控制器D,A和B.

可用性

適用於iOS 3.0及更高版本。

UINavigationController類參考

您必須在UserDefaults存儲應用程序存在時的狀態。 您可以使用app delegate方法- (void)applicationWillTerminate:(UIApplication *)application

然后,當再次啟動應用程序時,您將從UserDefaults檢索該信息,並初始化您希望在視圖控制器層次結構中擁有的視圖控制器。 然后使用以下方法將它們添加到UINavigationControllersetViewControllers:animated: .

使用popToViewController。

navigationController.viewControllers = [NSArray arrayWithObjects:firstVC, secondVC, thirdVC, nil];
[navigationController popToViewController:thirdVC animated:NO];

注意:確保將動畫設置為NO。

您仍然會像平常一樣啟動導航控制器,但在顯示之前將第二個和第三個視圖控制器推到它上面。 因此,如果您在app delegate的應用程序中執行此操作:didFinishLaunchingWithOptions:

UIViewController *firstController = [[[FirstViewCon alloc] initWithNibName:@"MyFirstViewCon" bundle:nil] autorelease];
UIViewController *secondController = [[[SecondViewCon alloc] initWithNibName:@"SecondViewCon" bundle:nil] autorelease];
UIViewController *thirdController = [[[ThirdViewCon alloc] initWithNibName:@"ThirdViewCon" bundle:nil] autorelease];

UINavigationController *theNavCon = [[[UINavigationController alloc] initWithRootViewController:firstController] autorelease];

[theNavCon pushViewController:secondController animated:NO];
[theNavCon pushViewController:thirdController animated:NO];

self.window.rootViewController = theNavCon;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM