在呈现视图控制器之前,IBOutlet 在 Objective C 中为零
IBOutlet are nil in Objective C before the view controller is presented
UIStoryboard *gamePlaySB = [UIStoryboard storyboardWithName:STORYBOARD_GAME_PLAY bundle:nil];
CustomAlertViewController *customAlertVC = [gamePlaySB instantiateViewControllerWithIdentifier:CUSTOM_ALERT_VIEW_CONTROLLER];
我已经如上所示初始化了 viewcontroller
,但是当尝试从初始化的 viewcontroller
访问 IBOutlet
标签并设置其文本时,标签为 nil。
所以,我必须将值传递给一个变量,然后在 viewDidLoad
中使用它来设置标题。还有其他简单的方法吗?
CustomAlertViewController *customAlertVC = [gamePlaySB instantiateViewControllerWithIdentifier:CUSTOM_ALERT_VIEW_CONTROLLER];
__unused UIView* view = customAlertVC.view; // this will load everything and call viewDidLoad
customAlertVC.property.text = @"blahblah";
如果真的想从外面设置的话。但是根据@D。 Mika 回答,这是不可取的。
ViewController 的目的是管理视图的元素。 ViewController 的 "user" 不应该知道视图的结构,因此也不知道所使用的出口。因此,我认为您想要访问 ViewController 之外的网点没有用。
通过 ViewController 的属性提供要显示的数据的方式很常见,也很有用。
为了完整起见,应该提到您可以通过访问视图来强制加载视图控制器的视图 属性。但是,正如我所说,这不是一种合适的技术。 :-)
I have initialised the view controller as shown above but when trying
to access the IBOutlet label from the initialised view controller and
set its text, the label is nil.
那是因为视图 (UILabel
) 在您成为 presenting/pushing 控制器之前尚未初始化。
So, I have to pass the value to a variable and then use that in the
viewDidLoad to set the title. Is there any other easy way of doing
this?
是的。是的。如果您声明视图 (UILabel
) lazily,您可以直接将值传递到那里,如下所示:
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let vc2 = VC2()
vc2.label.text = "HELLO"
self.present(vc2, animated: true, completion: nil)
}
}
class VC2: UIViewController {
lazy var label: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
label.textColor = .gray
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .red
self.view.addSubview(self.label)
self.label.center.equalTo(self.view.center)
}
}
UIStoryboard *gamePlaySB = [UIStoryboard storyboardWithName:STORYBOARD_GAME_PLAY bundle:nil];
CustomAlertViewController *customAlertVC = [gamePlaySB instantiateViewControllerWithIdentifier:CUSTOM_ALERT_VIEW_CONTROLLER];
我已经如上所示初始化了 viewcontroller
,但是当尝试从初始化的 viewcontroller
访问 IBOutlet
标签并设置其文本时,标签为 nil。
所以,我必须将值传递给一个变量,然后在 viewDidLoad
中使用它来设置标题。还有其他简单的方法吗?
CustomAlertViewController *customAlertVC = [gamePlaySB instantiateViewControllerWithIdentifier:CUSTOM_ALERT_VIEW_CONTROLLER];
__unused UIView* view = customAlertVC.view; // this will load everything and call viewDidLoad
customAlertVC.property.text = @"blahblah";
如果真的想从外面设置的话。但是根据@D。 Mika 回答,这是不可取的。
ViewController 的目的是管理视图的元素。 ViewController 的 "user" 不应该知道视图的结构,因此也不知道所使用的出口。因此,我认为您想要访问 ViewController 之外的网点没有用。 通过 ViewController 的属性提供要显示的数据的方式很常见,也很有用。
为了完整起见,应该提到您可以通过访问视图来强制加载视图控制器的视图 属性。但是,正如我所说,这不是一种合适的技术。 :-)
I have initialised the view controller as shown above but when trying to access the IBOutlet label from the initialised view controller and set its text, the label is nil.
那是因为视图 (UILabel
) 在您成为 presenting/pushing 控制器之前尚未初始化。
So, I have to pass the value to a variable and then use that in the viewDidLoad to set the title. Is there any other easy way of doing this?
是的。是的。如果您声明视图 (UILabel
) lazily,您可以直接将值传递到那里,如下所示:
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let vc2 = VC2()
vc2.label.text = "HELLO"
self.present(vc2, animated: true, completion: nil)
}
}
class VC2: UIViewController {
lazy var label: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
label.textColor = .gray
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .red
self.view.addSubview(self.label)
self.label.center.equalTo(self.view.center)
}
}