2016-10-24 34 views
1

我目前正在開發使用的是iOS 10和斯威夫特3iOS的10斯威夫特3的UIViewController目前不起作用

我認爲我可能毀了我的控制器之間的導航應用。

事實上,當我嘗試呈現一個新的視圖控制器時,我在Xcode調試器上有這個警告。

警告:嘗試在其視圖不在窗口層次結構中的Project.StoryBoardManager上呈現FirstViewController!

我做了一些研究,但我無法修復我的錯誤。

我有這對我的AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

    let storyboard = UIStoryboard(name:"Authentication", bundle: nil) 
    let vc = storyboard.instantiateInitialViewController() as UIViewController! 

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

    return true 
} 

這對我的課提出意見的消息

class StoryBoardManager: UIViewController{ 

fileprivate var appD = UIApplication.shared.delegate as! AppDelegate 

func changeView(storyboardName: String){ 

    let storyboard = UIStoryboard(name: storyboardName, bundle: nil) 
    if let vc = storyboard.instantiateInitialViewController() { 

     vc.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal 
     vc.modalPresentationStyle = UIModalPresentationStyle.fullScreen 

     //appD.window?.rootViewController = vc 
     present(vc, animated: true, completion: nil) 

    } else { 
     print("Unable to instantiate VC from \(storyboardName) storyboard") 
    } 
    } 
override func viewDidLoad(){ 
    super.viewDidLoad() 
} 

如果我評論RootViewController的的更新的新控制器沒有出現。

編輯@Zac關

import Foundation 
import UIKit 

class CustomNavBar: UIView { 

    fileprivate let _storyBoardManager : StoryBoardManager = StoryBoardManager() 
fileprivate var _currentUIViewController : UIViewController = UIViewController() 

init() { 
    super.init(frame: CGRect(x: 0, y: 0, width:0, height:0)) 
} 

func changeViewStoryboard(sender: UIButton!){ 

    if (sender.tag == 0){ 
     self._storyBoardManager.changeView(storyboardName: "View1") 
    } else if (sender.tag == 1) { 
     self._storyBoardManager.changeView(storyboardName: "View2") 
    } else if (sender.tag == 2) { 
     self._storyBoardManager.changeView(storyboardName: "View3") 
    } else { 
     self._storyBoardManager.changeView(storyboardName: "View4") 
    } 
} 

override init(frame: CGRect) { 
    super.init(frame: frame) 
} 

func createButton(title: String, posX: Double, witdh: Double, tag: Int, font: UIFont) -> UIButton { 

    let buttonCreated = UIButton(frame: CGRect(x: posX, y: 0, width: witdh, height: 60)) 
    buttonCreated.setTitle(title, for: UIControlState()) 
    buttonCreated.setTitleColor(CommonVariable.darkGrey, for: UIControlState()) 
    buttonCreated.titleLabel!.font = font 
    buttonCreated.tag = tag 
    buttonCreated.addTarget(self, action:#selector(self.changeViewStoryboard(sender:)), for: UIControlEvents.touchUpInside) 

    buttonCreated.backgroundColor = UIColor.white 

    return buttonCreated 

} 

required init?(coder aDecoder: NSCoder) { 
    Super. Inuit (coder: decoder) 

    addSubview(self.createButton(title: « ChangeView », posX: 256.0, witdh: Double(self._sizeButton) - 1, tag: 1, font: UIFont(name: « Arial », size: 15)!)) 
    addSubview(self.createButton(title: « ChangeView 2 », posX: 512.0, witdh: Double(self._sizeButton) - 1, tag: 2, font: UIFont(name: « Arial », size: 15)!)) 
} 

} 
+0

的警告已經告訴你爲什麼不能存在。基本上,你的類'StoryBoardManager'目前不是窗口層次結構中的'ViewController',因此它不能提供一個新的vc。你可以展示更多關於'changeView'調用的代碼嗎? –

+0

@ZacKwan我有更新我的文章。 感謝您的幫助 –

回答

-1

請嘗試更改像下面的代碼:

class StoryBoardManager: UIViewController{ 

fileprivate var appD = UIApplication.shared.delegate as! AppDelegate 

func changeView(storyboardName: String){ 

    let storyboard = UIStoryboard(name: storyboardName, bundle: nil) 
    if let vc = storyboard.instantiateInitialViewController() { 

     vc.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal 
     vc.modalPresentationStyle = UIModalPresentationStyle.fullScreen 

     //appD.window?.rootViewController = vc 
     appD.present(vc, animated: true, completion: nil) 

    } else { 
     print("Unable to instantiate VC from \(storyboardName) storyboard") 
    } 
    } 
override func viewDidLoad(){ 
    super.viewDidLoad() 
}