如何解决使用Storyboard创建的ViewController?.instantiateViewControlleridentifier:“”无法访问发送ViewController的属性/功能
我知道隐式展开的可选值问题已经被回答了一百万次了,但这太奇怪了。我使用Storyboard?.instantiateViewController(identifier:“ SettingsViewController”)创建当前的视图控制器,但无法访问发送视图控制器的属性。我从发送视图控制器中对象的isHidden属性的可选值nil获得错误。我检查以确保所有必需的插座均已正确链接,并且据我所知。不知道发生了什么。这是isHidden为nil的代码:
@IBAction func hideSearchRadiusPressed(_ sender: UIButton) {
if hideSearchRadiusButton.titleLabel?.text == "Hide Search Radius" {
mapViewController.searchRadiusView.isHidden = true
mapViewController.searchRadiusView.isUserInteractionEnabled = false
hideSearchRadiusButton.titleLabel?.text = "Show Search Radius"
} else if hideSearchRadiusButton.titleLabel?.text == "Show Search Radius" {
mapViewController.searchRadiusView.isHidden = false
mapViewController.searchRadiusView.isUserInteractionEnabled = true
hideSearchRadiusButton.titleLabel?.text = "Hide Search Radius"
}
}
这是在其中实例化当前视图控制器(SettingsViewController)的代码。这是在MapViewController(初始视图控制器)中完成的:
@IBAction func settingsButtonPressed(_ sender: UIBarButtonItem) {
// Identifier is under 'Storyboard ID' in the identity inspector for SettingsViewController
guard let settingsViewController = storyboard?.instantiateViewController(identifier: "SettingsViewController") else {return}
settingsViewController.modalPresentationStyle = .overCurrentContext
settingsViewController.transitioningDelegate = self
present(settingsViewController,animated: true)
}
我在“ mapViewController.searchRadiusView.isHidden = true”行上得到了错误,但是我确定也为isUserInteractionEnabled部分得到了它。知道为什么在isHidden属性上会发生此错误吗?再次正确链接了searchRadiusView的出口(在另一个视图控制器中,这就是为什么我将其称为mapViewController的一部分)。感谢您的帮助。
解决方法
好吧,我解决了自己的问题。如果其他人遇到此问题,我将对此进行更广泛的介绍。如果像我以前那样以编程方式实例化所谓的“ newViewController”,则需要添加一个过渡委托:
class SendingViewController: UIViewController {
@IBAction func sendingButtonPressed(_ sender: UIBarButtonItem) {
// Identifier argument of 'instantiateViewController' is under 'Storyboard ID' in the identity inspector for NewViewController
guard let newViewController = storyboard?.instantiateViewController(identifier: "NewViewController") else {return}
// Presentation style up to you. I did it this way because I am using a custom transition
newViewController.modalPresentationStyle = .overCurrentContext
// Set the newViewController's transitioningDelegate property to self (the sending view controller)
newViewController.transitioningDelegate = self
present(newViewController,animated: true)
}
}
在NewViewController中,当您想访问SendingViewController的属性时,您要做的就是引用过渡委托:
class NewViewController: UIViewController {
@IBAction func buttonPressed(_ sender: UIButton) {
// Remember,we set the current view controller's transitioningDelegate as SendingViewController before,when we were INSIDE SendingViewController,by using newViewController.transitioningDelegate = self
if let sendingVC = self.transitioningDelegate as? SendingViewController {
// Now we can access properties of the sendingVC.
// In my case,I needed to access a UIView,put your code below in place of mine
sendingVC.customView.isHidden = true
sendingVC.customView.isUserInteractionEnabled = false
dismiss(animated: true,completion: nil)
}
}
}
希望这对某人有帮助。
,让settingsViewController = Storyboard?.instantiateViewController(identifier:“ SettingsViewController”)为! ClassName_for_That_Identifier_Here
这将解决问题!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。