2016-10-24 1 views
1

Je développe actuellement une application utilisant iOS 10 et Swift 3iOS 10 Swift 3 UIViewController actuelle ne fonctionne pas

Je pense que je peux avoir détruit la navigation entre mes contrôleurs.

En effet, lorsque j'essaie de présenter un nouveau contrôleur de vue, j'ai cet avertissement sur le débogueur Xcode. Avertissement: Essayez de présenter FirstViewController sur Project.StoryBoardManager dont la vue ne figure pas dans la hiérarchie des fenêtres! J'ai fait des recherches mais je ne suis pas capable de réparer mon bug.

J'ai sur mon 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 
} 

Et ceci sur ma classe pour présenter des vues nouvelles

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

Si je commente la mise à jour RootViewController le nouveau contrôleur ne se présente pas .

EDIT pour @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)!)) 
} 

} 
+0

L'avertissement vous a dit pourquoi il ne peut pas présenter. Fondamentalement, votre classe 'StoryBoardManager' n'est pas actuellement' ViewController' dans la hiérarchie des fenêtres, donc elle ne peut pas présenter un nouveau vc. Pouvez-vous montrer plus de code sur comment 'changeView' est appelé? –

+0

@ZacKwan J'ai mis à jour mon message. Merci pour l'aide –

Répondre

-1

S'il vous plaît essayez de modifier le code comme ci-dessous:

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