簡體   English   中英

iOS無法關閉視圖控制器

[英]iOS Can't dismiss view controller

我的應用程序有問題。 場景很簡單,成功創建帳戶后,我不想關閉當前頁面或導航到登錄頁面。 我的故事板看起來像這樣:

在此處輸入圖片說明

成功創建帳戶后,我有一個彈出窗口,其中包含一些關於它的信息,我們向您發送驗證電子郵件,在此彈出窗口之后,我想轉到左起第二個頁面 - 這是我的主應用程序頁面(現在稱為“視圖控制器”)。

我試過關閉窗口,但我在那里沒有效果,它只能關閉我的彈出窗口。 當我嘗試重定向時,按下后退按鈕時出現問題,它會導致注冊頁面。 有一些代碼:

// Create new user and send verification email

        Auth.auth().createUser(withEmail: userEmail, password: userPassword) { user, error in if error == nil && user != nil {
            self.sendVerificationMail();
            self.displayAlertMessage(alertTitle: "Success", alertMessage: "Your account created successfully. We send you a verification email.");

            // Redirect to View Controller

            } else {
            self.displayAlertMessage(alertTitle: "Unhandled error", alertMessage: "Undefined error #SignUpViewController_0002");
            }
        }

...

func displayAlertMessage(alertTitle: String, alertMessage:String, alertRedirection:String = ""){
        let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert);

        let okAction = UIAlertAction(title:"Ok", style: UIAlertAction.Style.default, handler: nil);

        alert.addAction(okAction);

        self.present(alert, animated:true, completion:nil);
    }

如果我添加這個:

self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)

警報之后,它只關閉警報,警報之前,它什么都不做(與解雇相同)。

要關閉並彈出到主視圖,您可以使用警報按鈕操作處理程序。

alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: { (action) in

            self.dismiss(animated: true, completion: {
                self.navigationController?.popToRootViewController(animated: true)
            })
        }))

或者您可以使用以下幾行導航到特定的視圖控制器。

for viewController in self.navigationController!.viewControllers {
            if viewController.isKind(of: <Your_Main_ViewController_Class_Name>.self) {
                self.navigationController?.popToViewController(viewController, animated: true)
                break
            }
        }

Your_Main_ViewController_Class_Name是導航控制器堆棧中需要導航到的視圖控制器。 (即)主視圖

要在顯示警報彈出窗口后盲目導航到主視圖,您可以在顯示警報時使用完成處理程序。

self.present(alert, animated: true, completion: {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
                self.navigationController?.popToRootViewController(animated: true)
            }
        })

好吧,您正在使用導航控制器,例如,為了“呈現”一個新的視圖控制器,您需要推送它。

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("IDOFYOURVIEW") as CLASS_NAME_OFYOUR_VIEWCONTROLLER
navigationController?.pushViewController(vc, animated: true)

使用最后一個代碼,您可以“呈現”(推送)一個新的視圖控制器

現在,如果您想在按下后退按鈕時進行其他操作,請嘗試使用此行

override func viewDidLoad {
    super.viewDidLoad()
    self.navigationItem.hidesBackButton = true
    let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Bordered, target: self, action: "back:")
    self.navigationItem.leftBarButtonItem = newBackButton
}

func back(sender: UIBarButtonItem) {
    //in this part you can move to other view controller, examples
    // Go back specific view in your navigation controller
    for controller in self.navigationController!.viewControllers as Array {
    if controller.isKind(of: NAMECLASS_OFYOUR_VIEWCONTROLLER.self) {
        _ =  self.navigationController!.popToViewController(controller, animated: true)
        break
       }
    }
    // Perform your custom actions
    // ...
    // Go back to the previous ViewController
    self.navigationController?.popViewControllerAnimated(true)
}

問候

視圖控制器類:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
        self.performSegue(withIdentifier: "notLoggedView", sender: self);
    }
}

類 ID 設置為“測試”:

在此處輸入圖片說明

“推”:

...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "test") as ViewController
            navigationController?.pushViewController(vc, animated: true)
...

可悲的是錯誤:

“UIViewController”不可轉換為“ViewController”

我哪里出錯了?

暫無
暫無

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

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