我正在將基於PUREMVC的應用程序從objective-c重新編碼到Swift中。Swift - UIButton event not firing
我現在正在創建一個表單並提交按鈕,但它沒有任何事件觸發。正在編輯的文本字段,不是按下按鈕。文本字段上的鍵盤仍然顯示爲觸摸,但沒有事件觸發。這裏是我的代碼:
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("::")
}
}
如果你想看到的Util我用它來創建的元素:
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
}
真是離奇,沒有事件被觸發。任何人?
您是否已將選擇器添加到您的控件中? – a4arpan
在嘗試了下面的所有內容之後,沒有任何幫助,我將整個視圖代碼複製到一個新的swift項目中,並且一切正常。所以我還沒瘋。但問題是,什麼阻止了viewcontroller行爲像這樣?我希望這在我的PuremVC框架內工作。 –
你怎麼把這個連接到你的故事板? – adolfosrs