【发布时间】:2017-10-13 08:13:11
【问题描述】:
我想管理内存,所以我想在离开 ViewController 时取消 UIview。
我尝试使用关键字“weak”,但由于我的聊天键盘为 nil,所以我崩溃了。
我不知道为什么让它崩溃。
谢谢。
class ChatKeyboard: UIView {
var buttonMic:UIButton = { ()->UIButton in
let ui:UIButton = GeneratorButton()
return ui
}()
override init(frame: CGRect) {
super.init(frame: frame)
print("===) ChatKeyboard init.")
translatesAutoresizingMaskIntoConstraints = false
loadContent()
loadConstrain()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
deinit {
print("===) ChatKeyboard deinit.")
}
func loadContent() {
backgroundColor = UIColor.white
addSubview(buttonMic)
}
func loadConstrain() {
buttonMic.snp.makeConstraints { (make) -> Void in
make.left.equalTo(micLeftPadding)
make.top.equalTo(micTopPadding)
make.width.equalTo(UIScreen.main.bounds.width*0.0581)
make.height.equalTo(UIScreen.main.bounds.height*0.045)
}
}
}
class ChatroomViewController: UIViewController{
weak var chatKeyboard:ChatKeyboard?
override func viewDidLoad() {
super.viewDidLoad()
chatKeyboard = ChatKeyboard(frame: CGRect(x: 0, y: 0, width: 300, height: 44))
}
}
我在“chatKeyboard = ChatKeyboard(frame: CGRect(x: 0, y: 0, width: 300, height: 44))”处设置断点,然后我的日志打印:
===) ChatKeyboard init.
===) ChatKeyboard deinit.
【问题讨论】:
标签: ios swift uiview initialization