使用标识符使用自定义init实例化View Controller

时间:2014-11-26 08:44:24

标签: ios swift uiviewcontroller instantiation

如何使用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.
}

}

1 个答案:

答案 0 :(得分:2)

我认为你想做的事情是不可能的。 但是你可以做这样的事情

let destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ProfileViewController") as ProfileViewController
destViewController.userId = "abc"