Skip to content

damienromito/NSObject-FromClassName

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

NSObject-FromClassName

A swift extension, to get quickly an NSObject from its classname

extension NSObject {
    
    class func fromClassName(className : String) -> NSObject {
        let className = NSBundle.mainBundle().infoDictionary!["CFBundleName"] as! String + "." + className
        let aClass = NSClassFromString(className) as! UIViewController.Type
        return aClass.init()
    }
    
}

Exemple with a viewController I get from a string array

override func viewDidLoad() {
    super.viewDidLoad()

    let controllers = ["SettingsViewController", "ProfileViewController", "PlayerViewController"]
    self.presentController(controllers.firstObject as! String)

}

func presentController(controllerName : String){
	let nav = UINavigationController(rootViewController: NSObject.fromClassName(controllerName) as! UIViewController )
    nav.navigationBar.translucent = false
    self.navigationController?.presentViewController(nav, animated: true, completion: nil)
}

About

In swift, get quickly an NSObject from its classname

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages