martes, 4 de abril de 2017

iOS: Instantiate viewcontrollers from strings with check

let storyboard = UIStoryboard(name: "Main", bundle: nil)
                   
var viewController: UIViewController?
                   
if let _ = NSClassFromString("<app_name>.\(menuItem.name!)ViewController") as? UIViewController.Type {
let vc = storyboard.instantiateViewController(withIdentifier: "\(menuItem.name!)ViewController")
viewController = vc
                       
} else  {
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
vc.name = menuItem.name!
viewController = vc
}
     
if let navigationcontroller = self.revealViewController().frontViewController as? UINavigationController {
                       
if type(of:viewController!) != type(of: navigationcontroller.topViewController!) {
navigationcontroller.topViewController!.revealViewController().revealToggle(animated: true)
navigationcontroller.pushViewController(viewController!, animated: true)
}
}

No hay comentarios:

Publicar un comentario