如何以编程方式在swift中使用storyboard id获取当前viewcontroller(当前正在呈现)?

时间:2016-04-28 11:39:46

标签: ios swift uiviewcontroller presentmodalviewcontroller

**如何在swift中获取呈现视图控制器。我正在使用storyboard id。**为了呈现下一个viewcontroller,我该如何添加到层次结构?

我试过这样:

   let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
   let logincontroller  = mainStoryboard.instantiateViewControllerWithIdentifier("LoginViewController")

    if UIApplication.sharedApplication().keyWindow?.rootViewController.presentingViewController == logincontroller {

     let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
     let nextVC  = mainStoryboard.instantiateViewControllerWithIdentifier(storayboardIdentifier)

     self.presentViewController(nextVC, animated: true, completion: nil)

     }

在Appdelegate.swift中

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

         if isLoggedin == false {

        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

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

        let initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController")

        self.window?.rootViewController = initialViewController


        self.window?.makeKeyAndVisible()

        return true
    }
    else {

        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

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

        let initialViewController = storyboard.instantiateViewControllerWithIdentifier("HomeViewController")

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

        return true
 }

}

代码有效,它显示视图但仍有警告

  

无法为其实例化默认视图控制器   UIMainStoryboardFile'Main' - 也许是指定的入口点   没设置?

4 个答案:

答案 0 :(得分:1)

检查初始入口点。

另外,在代码练习下呈现屏幕是更好的方法。

let objNextViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ID_NextViewController") as? ViewController

self.presentViewController(objNextViewController, animated: true) { () -> Void in
            print("Achive")
}

enter image description here

已编辑:

如果要从已呈现的视图控制器中呈现viewcontroller,请使用下面的代码。

var currentViewController : UIViewController!


let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

if let viewControllers = appDelegate.window?.rootViewController?.presentedViewController 
{

    self.currentViewController = viewControllers as UIViewController

}
else if let viewControllers = appDelegate.window?.rootViewController?.childViewControllers 
{

    self.currentViewController = viewControllers.last! as UIViewController

}


let objNewViewController : NewViewController!

    self.currentViewController.presentViewController(objNewViewController!, animated: true, completion: { () -> Void in
                        NSLog("Achived")

})

答案 1 :(得分:0)

使用故事板名称尝试presentingViewController的此代码:

let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") as UIViewController

self.presentViewController(viewController, animated: false, completion: nil)

答案 2 :(得分:0)

试试这段代码,

let storyboard : UIStoryboard = UIStoryboard(name:"Main", bundle: nil)
let vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController    
let navigationController = UINavigationController(rootViewController: vc) 
self.presentViewController(navigationController, animated: true, completion: nil)

希望它有用

答案 3 :(得分:0)

我的猜测是你没有为你的故事板设置一个初始视图控制器。

要解决此问题,请打开故事板并选择要作为第一个显示的UIViewController。然后在Attributes inspector标签(公用程序面板右侧的第三个标签)中,查看View Controller部分下方,确保选中Is Initial View Controller复选框。