【发布时间】:2018-07-29 19:19:38
【问题描述】:
我一直在试验和练习以编程方式构建应用程序,但我无法理解为什么我的根 VC 中的 UIButton 在模态转场后仍保留在下一个视图中。当我通过情节提要执行此操作时,视图中的所有元素都会消失。
我的 rootVC 由一层和一个 UIButton 组成:
按下按钮模态显示下一个视图控制器,但 UIButton 仍然存在:
第二个 VC 文件中没有对 UIButton 的引用。下面是来自 rootVC 的代码:
var imInChinaButton : UIButton {
var button = UIButton( frame: CGRect(
x: 0,
y: 0,
width: 300,
height: 300))
button.setImage(#imageLiteral(resourceName: "button"), for: .normal)
button.setTitle("hello", for: .normal)
button.contentMode = UIViewContentMode.scaleAspectFit
button.center = view.center
button.backgroundColor = .clear
button.isUserInteractionEnabled = true
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
return button
}
self.view.addSubview(imInChinaButton)
}
@objc func buttonTapped(_ sender: UIButton) {
sender.setImage(#imageLiteral(resourceName: "pressedButton"), for: .normal)
let resultsViewController = ResultsViewController()
self.navigationController!.present(resultsViewController, animated: true, completion: nil)
}
任何帮助都会很棒!非常感谢您的宝贵时间。
【问题讨论】: