从AppDelegate调用函数并使用该函数执行segue

问题描述 投票:0回答:2

我正试图从AppDelegate调用一个函数,并使用该函数我正在尝试执行segue。

My Storyboard (点击图片放大)

func doSegue (_ verification : Bool) {
    if verification {
        print ("Segue performed")
        LoginVC ()
        .performSegue (withIdentifier: "tosignup", sender: nil)
    }
    else { print("An error occured while login") }
}
func btnremove () {
    print ("Segue performed 1")
    loginbtn.isHidden = true doSegue (true)
}

这些是超出执行的错误。

These are the errors which came beyond execution.

ios swift uiviewcontroller appdelegate
2个回答
0
投票

如果在LoginVC中没有对prepareForSegue做任何特殊操作,可以在AppDelegate中启动根视图控制器:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    if (verification) {
        let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginVC")

        self.window?.rootViewController = initialViewController
        self.window?.makeKeyAndVisible()
    } else {
        let initialViewController = storyboard.instantiateViewController(withIdentifier: "SignupformVC")

        self.window?.rootViewController = initialViewController
        self.window?.makeKeyAndVisible()
    }

    return true
}

您还应该为视图控制器设置标识符。


0
投票

根据这里的注释,这是一个简单的方法,可以将对现有登录视图控制器的引用放入应用程序委托中,以便您可以对其执行segue。

首先在app委托中你需要为登录视图控制器添加一个属性,如下所示:

public var loginVC: LoginVC?

现在在登录视图控制器中,您可以在viewDidLoad中按如下方式设置此属性:

    // Get the app delegate ensuring it is the right type.
    if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
        appDelegate.loginVC = self
    }

然后回到app委托中,您可以在doSegue方法中使用该属性,如下所示:

func doSegue (_ verification : Bool) {
    // Ensure that the loginVC property has been set and if not we can't perform the segue.
    guard let loginVC = self.loginVC else {
        print ("The login view controller is not presented")

        return
    }

    if verification {
        print ("Segue performed")
        // Use the property to perform the segue.
        loginVC.performSegue(withIdentifier: "tosignup", sender: nil)
    }
    else { print("An error occured while login") }
}

现在这可能不是最好的方法,但是如果不了解项目的更多信息,这是一个让它工作的简单方法。

© www.soinside.com 2019 - 2024. All rights reserved.