
[英]Instantiating Modal view controller freezes the app - Probable issue with Xcode 7
[英]Xcode: instantiating a view controller with custom variable
所以我想从 storyboard 实例化一个视图 controller 并更改它的 static 变量。
这是“vc1”——要实例化的视图 controller:
import UIKit
class vc1: UIViewController {
@IBOutlet weak var lbl_title: UILabel!
static var title = "initial value"
override func viewDidLoad() {
super.viewDidLoad()
lbl_title.text = vc1.title
}
}
这是我的根 vc。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var btn_go: UIButton!
@IBAction func btn_gogogo(_ sender: Any) {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "vc1") as! vc1
vc.title = "bla"
self.present(vc, animated: true, completion: nil)
}
}
在这里,我试图更改我刚刚实例化的视图 controller 的 static 变量,但没有任何效果。 变量(在我的例子中为 'title' )始终保持其初始值。
这里有什么问题?
最好
标记
不要试图覆盖视图控制器的title
属性。 相反,创建你自己的:
class vc1: UIViewController {
@IBOutlet weak var lbl_title: UILabel!
var myTitle = "initial value"
override func viewDidLoad() {
super.viewDidLoad()
lbl_title.text = myTitle
}
}
class ViewController: UIViewController {
@IBOutlet weak var btn_go: UIButton!
@IBAction func btn_gogogo(_ sender: Any) {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "vc1") as! vc1
vc.myTitle = "bla"
self.present(vc, animated: true, completion: nil)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.