我写了一个类,其中显示的视图有UITextView
和几个按钮。一切正常,但按钮不响应水龙头。以下是我认为我班上的相关代码:
class FullPrompt:UIViewController {
var canceledPressedCallback:(()->()?)?=nil
var okPressedCallback:((_ txt: String)->()?)? = nil
var popupView = UIView()
var field = UITextView()
var parentView = UIView()
func prompt(_ message:String,view: UIViewController,numberInput: Bool, callback: @escaping (_ txt: String)->()) {
prompt(message, view: view, numberInput: numberInput, callback: callback,cancelCallback: nil,defaultText: "")
}
func prompt(_ prompt:String,view: UIViewController,numberInput: Bool, callback: @escaping (_ txt: String)->(), cancelCallback: (()->())?,defaultText: String) {
canceledPressedCallback=cancelCallback
okPressedCallback=callback
let cancelButton=UIButton(frame: CGRect(x: 10, y: y, width: 70, height: 32))
cancelButton.setTitle("Cancel", for: .normal)
cancelButton.addTarget(self, action: #selector(cancelButtonPressed(_:)), for: .touchUpInside)
popupView.addSubview(cancelButton)
let okButton=UIButton(frame: CGRect(x: 210, y: y, width: 70, height: 32))
okButton.setTitle("Ok", for: .normal)
okButton.setTitleColor(UIColor.blue, for: .normal)
okButton.addTarget(self, action: #selector(okButtonPressed(_:)), for: .touchUpInside)
popupView.addSubview(okButton)
view.view.addSubview(popupView)
}
func cancelButtonPressed(_ sender: UIButton ) {
popupView.removeFromSuperview()
if canceledPressedCallback != nil {
canceledPressedCallback!()
}
}
func okButtonPressed(_ sender: UIButton) {
popupView.removeFromSuperview()
okPressedCallback!(field.text)
}
}
我做错了什么?
答案 0 :(得分:1)
我明白了。我的错误是在我的班级中添加了一个视图属性(popupView)。由于我的类是UIViewController的子类,我需要将我的子视图添加到类的view属性,而不是创建另一个视图。所以,基本上,无论我在哪里添加一些东西到popupView,我只是用self.view替换它。它们按钮工作正常。