如何使用storyboard方法实例化自定义ViewController
的自定义init
课程?
如何调用ProfileViewController(userId: "abc")
init?
实例化视图控制器
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ProfileViewController") as UIViewController
自定义视图控制器类
class ProfileViewController: UIViewController {
var userId = ""
init(userId: String) {
self.userId = userId
super.init(nibName: nil, bundle: nil)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
let menuItem = UIBarButtonItem(image: UIImage(named: "MenuIcon"), style: .Plain, target: self, action: "menuBarButtonItemClicked")
menuItem.tintColor = UIColor.whiteColor()
self.navigationItem.leftBarButtonItem = menuItem
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:2)
我认为你想做的事情是不可能的。 但是你可以做这样的事情
let destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ProfileViewController") as ProfileViewController
destViewController.userId = "abc"