我正在以編程方式創建一個按鈕,當我向其中添加“button.addTarget”代碼行時,我創建的每個按鈕都會收到此警告。代碼如下:
let logoutButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Log Out", for: .normal)
button.setWidth(width: 100)
button.setHeight(height: 30)
button.layer.cornerRadius = 15
button.backgroundColor = .white
button.setTitleColor(.red, for: .normal)
button.addTarget(self, action: #selector(handleLogOut), for: .touchUpInside)
return button
}()
我得到的警告是:'self'指的是方法'HomeController.self',這可能是意外的
它還附帶了一個修復程式,即:使用 'HomeController' 使此警告靜音
但是,當我實施此修復時,“button.addTarget”行的第一個引數中的“self”變為“HomeController.self”并導致應用程式崩潰。
我總是用“自我”來做這個,直到最近它才發出警告。現在它每次都會發出警告。當我使用“self”時,它仍然可以正常作業,但是 Xcode 中的警告很煩人......感謝任何幫助。
uj5u.com熱心網友回復:
使用lazy var 而不是讓它解決問題或在viewDidLoad 中設定目標。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/452643.html
上一篇:.NetMVC5多選串列框
