![](/img/trans.png)
[英]Programmatically push to view controller [2] in a navigation stack, and unwind to [1]
[英]How to unwind to the first view controller on a navigation stack
我有一個問題,將我的視圖展開到登錄屏幕。 故事板結構如下: 故事板結構
該應用程序的用戶流程如下:用戶登錄LoginVC->通過模式segue->在每個標簽欄項目上轉到主標簽欄屏幕,我在導航控制器上添加右欄按鈕以訪問配置文件頁面,每個標簽欄item具有獨立的導航控制器,使導航控制器結構保持線性。 - >一旦我點擊個人資料頁面按鈕,個人資料頁面以模態方式顯示 - >點擊個人資料頁面上的注銷按鈕時,它會觸發展開segue並關閉視圖控制器
func logoutUser(){
//Networking.logoutUser()
print("It goes to login")
self.performSegue(withIdentifier: "unwindToLogin", sender: self)
}
展開segue是在最左邊的VC.I連接展開segue上的LoginVC上實現的,並將其稱為“unwindToLogin”我只是使用performSegueWithIdentifier。 但是,該方法不會被調用,視圖也不會發生任何事情。
編輯1:我想知道,因為我在標簽欄vc上模擬調用配置文件頁面,它無法找到unwindtologin。 如果我只是使用instantiateviewcontoller來調用login,它會清除我的視圖控制器堆棧嗎?
編輯2:抱歉,我忘了當我檢查用戶是否登錄時,我使用下面的代碼:
if (FIRAuth.auth()?.currentUser != nil) {
self.storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "TabBarViewController")
}
實際上他們找不到unwind方法,因為根視圖不是主登錄視圖控制器。
有人可以幫忙嗎?
使用展開segue移動到任何View控制器。
單擊按鈕時移動到任何視圖控制器。
- (IBAction)unwindToCurrentController:(UIStoryboardSegue *)unwindSegue {}
現在構建並運行您的應用程序。 它完美地運作。
您需要在要調用它的每個ViewController上創建展開segue。 它們都可以在登錄屏幕中使用相同的@IBAction
目的地,它們都可以稱為"unwindToLogin"
。
我在MainTabBarController
上看到了unwind segue。 在UserProfileController
,您需要從視圖Controller圖標控制 -drag到Exit圖標,然后在彈出窗口中選擇您的@IBAction
。 然后在Document Outline中的UserProfileController
中找到unwind segue,並使其標識符為"unwindToLogin"
。
回應你的編輯2:
由於您的初始viewController是以編程方式放置的,因此無法展開到LoginViewController
。 在這種情況下,將着陸@IBAction
放入MainTabBarController
並讓它將self.window?.rootViewController
設置為登錄屏幕。
你可以試試這個為我工作。
[self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
我用它來消除模態視圖,以便我可以返回到原始視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.