【发布时间】:2017-11-24 23:16:40
【问题描述】:
好吧,我只是遇到了一些奇怪的事情。我的应用程序控制器依赖项将视图(标题)注入视图控制器。该视图控制器以模态方式呈现另一个视图控制器,并且依赖项注入它自己的标头以供呈现视图控制器使用。但是当它出现时,第一个控制器的标题消失了。
该属性仍处于设置状态,但已从视图层次结构中移除。
我已经在新的单视图项目中重现了这个问题:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
let button = UIButton(frame: CGRect(x: 0, y: 20, width: 100, height: 50))
button.setTitle("Click Me!", for: .normal)
button.addTarget(self, action: #selector(self.segue), for: .touchUpInside)
button.backgroundColor = .black
button.setTitleColor(.lightGray, for: .normal)
self.view.addSubview(button)
}
func segue() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
view.backgroundColor = .lightGray
let firstVC = FirstViewController()
firstVC.sharedView = view
present(firstVC, animated: false)
}
}
class FirstViewController: UIViewController {
var sharedView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.view.addSubview(self.sharedView)
let button = UIButton(frame: CGRect(x: 0, y: 200, width: 100, height: 50))
button.setTitle("Click Me!", for: .normal)
button.addTarget(self, action: #selector(self.segue), for: .touchUpInside)
button.backgroundColor = .black
button.setTitleColor(.lightGray, for: .normal)
self.view.addSubview(button)
}
func segue() {
let secondVC = SecondViewController()
secondVC.sharedView = self.sharedView
present(secondVC, animated: true)
}
}
class SecondViewController: UIViewController {
var sharedView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.view.addSubview(self.sharedView)
let button = UIButton(frame: CGRect(x: 0, y: 200, width: 100, height: 50))
button.setTitle("Click Me!", for: .normal)
button.addTarget(self, action: #selector(self.segue), for: .touchUpInside)
button.backgroundColor = .black
button.setTitleColor(.lightGray, for: .normal)
self.view.addSubview(button)
}
func segue() {
self.dismiss(animated: true)
}
}
有人能解释一下这里发生了什么吗?为什么 sharedView 从 FirstViewController 中消失了?
【问题讨论】:
-
在
addSubview()的文档中:Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.我认为这就是问题所在。你可能想要这个:stackoverflow.com/questions/4425939/can-uiview-be-copied -
嗯,有道理。有什么建议可以使这项工作正常进行吗?
-
我用一个可能的解决方案编辑了我之前的评论。但我会使用一种方法来创建带有所有自定义的 headerView 并调用它。
-
太棒了。看起来我只需要为我的标题制作一个工厂。谢谢!
-
@Larme,你打败了我。您应该发表您的评论作为答案。事实上,这是正确的答案。