【发布时间】:2016-08-03 22:04:12
【问题描述】:
背景:
我创建了一个 UIView (helloView) 的子类来在我的 MainViewController 视图上显示一些文本和一个按钮。
这个子类在MainVC中初始化viewDidLoad并添加到视图中:view.addSubview(helloView)
目标:helloView 有一个 UIButton (continueButton),目标为 dimissHelloView。这个方法简单地调用self.removeFromSuperView(),以便简单地在屏幕上获取helloView。
问题:用户与按钮的交互会破坏代码并返回 nil 指针异常(在展开 Optional 值时意外发现 nil)。尽管在 SO 和 Google 上搜索了几个小时,但我还是找不到通过其自己的按钮子视图关闭视图的正确方法。
以前的故障排除:调用removeFromSuperview时superview似乎是nil,但如果我使用断点并在控制台中调用它不是。
代码:忽略属性和约束部分,按钮创建和目标动作在底部。
import UIKit
class HelloView: UIView {
var helloText: String = ""
var continueButton: UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
didLoad()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
didLoad()
}
func didLoad(){
self.translatesAutoresizingMaskIntoConstraints = false
setContinueButton()
self.addSubview(continueButton)
}
override func didMoveToSuperview() {
setConstraints()
}
func setConstraints(){
//remove any previous constraints
for constraint in superview!.constraints {
if constraint.firstItem as! UIView == self && constraint.secondItem as? UIView == superview {
superview!.removeConstraint(constraint)
}
}
// Hello View basic constraints
let xAxisConstraint = NSLayoutConstraint(item: self, attribute: .CenterX, relatedBy: .Equal, toItem: superview, attribute: .CenterX, multiplier: 1, constant: 0)
let yAxisConstraint = NSLayoutConstraint(item: self, attribute: .CenterY, relatedBy: .Equal, toItem: superview, attribute: .CenterY, multiplier: 1, constant: 0)
// check device orientation and add constraints consequently
if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) {
let widthConstraint = NSLayoutConstraint(item: self, attribute: .Width, relatedBy: .Equal, toItem: superview, attribute: .Width, multiplier: 1, constant: -100)
let heightConstraint = NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .Equal, toItem: superview, attribute: .Height, multiplier: 1, constant: -150)
superview!.addConstraints([xAxisConstraint, yAxisConstraint, widthConstraint, heightConstraint])
}
if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation){
let widthConstraint = NSLayoutConstraint(item: self, attribute: .Width, relatedBy: .Equal, toItem: superview, attribute: .Width, multiplier: 1, constant: -100)
let heightConstraint = NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .Equal, toItem: superview, attribute: .Height, multiplier: 1, constant: -300)
superview!.addConstraints([xAxisConstraint, yAxisConstraint, widthConstraint, heightConstraint])
}
// Continue Button constraints
let centerXConstraint = NSLayoutConstraint(item: continueButton, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: continueButton, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: -8)
self.addConstraints([centerXConstraint, bottomConstraint])
}
func setContinueButton(){
continueButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
continueButton.setTitle("Continue", forState: .Normal)
continueButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
continueButton.addTarget(self, action: #selector(HelloView.dismissHelloView), forControlEvents: .TouchUpInside)
continueButton.translatesAutoresizingMaskIntoConstraints = false
}
func dismissHelloView(){
print(superview!)
self.removeFromSuperview()
}
}
【问题讨论】: