2016-04-28 38 views
0

**如何在swift中獲取呈現視圖控制器。我正在使用故事板ID。**並且爲了呈現下一個視圖控制器,我如何添加到層次結構中?如何獲取當前視圖控制器(當前呈現)使用故事板編號在swift progaramatically?

我想是這樣的:

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' - 可能指定的入口點是 沒有設置?

回答

0

試試這個代碼presentingViewController用故事板名稱:

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

self.presentViewController(viewController, animated: false, completion: nil) 
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) 

希望它有幫助

0

我的猜測是,你沒有設置您的故事板的初始視圖控制器。

爲了解決這個問題打開你的故事板,並選擇要顯示爲第一個的UIViewController。然後在Attributes inspector選項卡(公用程序面板右側的第三個選項卡)中查看View Controller部分,並確保已選中Is Initial View Controller複選框。

1

檢查初始入口點。

也呈現屏幕下面的代碼的做法是更好的辦法。

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

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

enter image description here

編輯:

如果你想從已經呈現的視圖控制器呈現視圖 - 控制然後在下面的代碼中使用。

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") 

}) 
+0

這將不完全編譯。 'presentViewController'不接受可選項,但是'objNextViewController'在這裏是可選的。另外,你可能需要'as? UIViewController'(儘管演員是不必要的)。 – nhgrif

+0

這必須是正確的答案 –

+1

感謝@AlessandroOrnano – Hasya

相關問題