简体   繁体   English

当前存在的iOS 10 Swift 3 UIViewController不起作用

[英]iOS 10 Swift 3 UIViewController present doesn't work

I'm currently developing an application using iOS 10 and Swift 3 我目前正在使用iOS 10和Swift 3开发应用程序

I think that I may have destroy the navigation between my controllers. 我认为我可能已经破坏了控制器之间的导航。

Indeed, when I try to present a new view controller, I have this warning on Xcode debugger. 确实,当我尝试提出一个新的视图控制器时,我在Xcode调试器上收到此警告。

Warning: Attempt to present FirstViewController on Project.StoryBoardManager whose view is not in the window hierarchy! 警告:尝试在Project.StoryBoardManager上显示其视图不在窗口层次结构中的FirstViewController!

I have made some research but I'm not able to fix my bug. 我进行了一些研究,但无法修复我的错误。

I have this on my AppDelegate.swift 我在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
}

And this on my class to present news views 这是我课堂上的新闻观点

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()
}

If I comment the update of rootViewController the new controller is not presented. 如果我评论rootViewController的更新,则不会显示新的控制器。

EDIT for @Zac Kwan 编辑@Zac Kwan

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)!))
 }

}

Please try changing the code like below : 请尝试更改如下代码:

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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM