我正在将基于 PUREMVC 的应用程序从 Objective-C 重新编码为 Swift。
我现在正在创建一个表单并提交按钮,但它没有触发任何事件。既没有编辑文本字段,也没有按下按钮。文本字段上的键盘仍然会在触摸时出现,但不会触发任何事件。这是我的代码:
class LoginViewController: UIViewController, UITextFieldDelegate{
var emailTF: UITextField!
var passwordTF: UITextField!
var submitBTN: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor.redColor()
// EMAIL
self.emailTF = ViewElements.formTextField(FORM_DEFAULT_USERNAME)
self.emailTF.tag = 100
self.view.addSubview(self.emailTF)
// PASSWORD
self.passwordTF = ViewElements.formTextField(FORM_DEFAULT_PASSWORD)
self.passwordTF.tag = 200
self.view.addSubview(self.passwordTF)
// SUBMIT
self.submitBTN = ViewElements.formButtonSubmit(FORM_SUBMIT_TEXT)
self.submitBTN.tag = 300
self.submitBTN.addTarget(self, action: "pressedButton", forControlEvents: UIControlEvents.TouchUpInside)
self.submitBTN.userInteractionEnabled = true
self.view.userInteractionEnabled = true
self.view.addSubview(self.submitBTN)
// layout
let tX = (SCREENWIDTH-NAVI_WIDTH)*0.5
let tY = SCREENHEIGHT*0.3
self.emailTF.frame = CGRect(x: tX, y: tY-NAVI_HEIGHT*1.2, width: NAVI_WIDTH, height: NAVI_HEIGHT)
self.passwordTF.frame = CGRect(x: tX, y: tY+NAVI_HEIGHT*0.2, width: NAVI_WIDTH, height: NAVI_HEIGHT)
self.submitBTN.frame = CGRect(x: tX, y: tY+NAVI_HEIGHT*1.6, width: NAVI_WIDTH, height: NAVI_HEIGHT)
// events
self.emailTF.delegate = self
self.passwordTF.delegate = self
print("---1---")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
visualEffectView.frame = self.introImage.bounds
self.introImage.addSubview(visualEffectView)
visualEffectView.alpha = 0.0
*/
// FORM EVENTS
func textFieldDidBeginEditing(textField: UITextField) {
print ("---")
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
print ("---")
return false
}
func pressedButton(sender: UIButton!) {
print("::")
}
}
如果您想查看我用来创建元素的实用程序:
class func formTextField(myText: String) -> UITextField
{
let newTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: NAVI_WIDTH, height: NAVI_HEIGHT));
newTextField.text = myText as String
newTextField.borderStyle = UITextBorderStyle.Line
newTextField.backgroundColor = UIColor.colorWithAlphaComponent(UIColor.whiteColor())(0.5)
newTextField.textAlignment = NSTextAlignment.Center
return newTextField
}
class func formButtonSubmit(myText: String) -> UIButton
{
let newButton: UIButton = UIButton()
newButton.setTitle(myText, forState: .Normal)
newButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
newButton.titleLabel!.font = UIFont(name: "InterstateCondensed", size: 45)
newButton.titleLabel!.textColor = UIColor.blackColor()
newButton.titleLabel!.textAlignment = .Center
newButton.backgroundColor = UIColor.colorWithAlphaComponent(UIColor.whiteColor())(0.8)
newButton.frame = CGRect(x: 0, y: 0, width: NAVI_WIDTH, height: NAVI_HEIGHT)
return newButton
}
没有任何事件触发,这真的很奇怪。有人吗?
最佳答案
尝试将 action: "pressedButton",
替换为 action: "pressedButton:",
或者如果您不使用 pressedButton
方法,请尝试删除它的参数。
关于ios - Swift - UIButton 事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34200831/