gpt4 book ai didi

ios - 使用带有自定义初始化的标识符实例化 View Controller

转载 作者:可可西里 更新时间:2023-11-01 01:43:11 26 4
gpt4 key购买 nike

我将如何使用 Storyboard方法使用自定义 init 实例化自定义 ViewController 类?

如何调用 ProfileViewController(userId: "abc") init?

实例化 View Controller

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ProfileViewController") as UIViewController

自定义 View Controller 类

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.
}

}

最佳答案

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

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

关于ios - 使用带有自定义初始化的标识符实例化 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27144644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com