如何使用IBAction从一个故事板移动到另一个故事板?

时间:2016-08-28 16:00:06

标签: ios iphone swift storyboard

我正在尝试使用连接到UIButton的IBAction回到Storyboard Main 在app app delegate中设置如下:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    if doneOnce == false {
        doneOnce = true
        window?.rootViewController = UIStoryboard.init(name: "Onboarding", bundle: nil).instantiateInitialViewController()
    } else {
        window?.rootViewController = UIStoryboard.init(name: "Main", bundle: nil).instantiateInitialViewController()
    }

    return true
}

当我在故事板“Onboarding”时,我试着回到故事板“Main”:
这些是我尝试过的3个代码片段。

    @IBAction func tutorialDone(sender: AnyObject) {


    let appDelegateTemp = UIApplication.sharedApplication().delegate as! AppDelegate
    appDelegateTemp.window?.rootViewController = UIStoryboard.init(name: "Main", bundle: nil).instantiateInitialViewController()


    //let storyboard = UIStoryboard(name: "Main", bundle: nil)
    //let vc = storyboard.instantiateInitialViewController()
    //showViewController(vc! as UIViewController, sender: self)

    //let storyboard = UIStoryboard(name: "Main", bundle: nil)
    //let vc = storyboard.instantiateViewControllerWithIdentifier("ViewController") as UIViewController
    //presentViewController(vc, animated: true, completion: nil)
}

但是当我按下触发tutorialDone的UIButton时,我收到了这个错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tutorialDone:]: unrecognized selector sent to instance

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以使用标识符:

从故事板启动视图控制器
    let storyboard = UIStoryboard(name: "myStoryboardName", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("nextViewController") as UIViewController
    presentViewController(vc, animated: true, completion: nil) 

别忘了给你的nextViewController提供ID

参考this_link