2015-12-10 119 views
0

我正在將基於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 
} 

真是離奇,沒有事件被觸發。任何人?

+0

您是否已將選擇器添加到您的控件中? – a4arpan

+0

在嘗試了下面的所有內容之後,沒有任何幫助,我將整個視圖代碼複製到一個新的swift項目中,並且一切正常。所以我還沒瘋。但問題是,什麼阻止了viewcontroller行爲像這樣?我希望這在我的PuremVC框架內工作。 –

+0

你怎麼把這個連接到你的故事板? – adolfosrs

回答

0

調用動作時可以使用Selector

self.submitBTN.addTarget(self, action: Selector("pressedButton"), forControlEvents: UIControlEvents.TouchUpInside) 

對於文本框的問題(我猜在這裏)的問題可能是您要添加的子視圖後in​​statiating委託。

// PASSWORD 
    self.passwordTF = ViewElements.formTextField(FORM_DEFAULT_PASSWORD) 
    self.passwordTF.tag = 200 
    self.passwordTF.delegate = self 
    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.emailTF.delegate = self 
    self.view.addSubview(self.submitBTN) 
+0

嘗試前後選擇器。沒有不同。在那裏也有委託申報,沒有區別。 –

0

嘗試action: "pressedButton:",

更換action: "pressedButton",或嘗試,如果你不使用它來除去pressedButton方法的參數。

0

你忘了pressedButton此之後添加:應該解決您的問題:

self.submitBTN.addTarget(self, action: Selector("pressedButton:"), forControlEvents: .TouchUpInside) 

而且全能的Xcode給你Storyboard,這將使你的生活變得更輕鬆,你會不會需要設置的所有參數代碼,甚至代理和數據源是從它設置的

+0

試過了,沒有解決任何問題。我喜歡編碼東西,所以我討厭故事板。我與一位編輯保持着聯繫,純粹而簡單。使事情變得更清潔,並在我的所有客觀項目中爲我完美工作。 –

0

我以爲我發佈了一個答案。問題是,我是如何將我的viewcontroller添加到窗口而不使用storyboard(我不喜歡)。所有上述代碼的想法是正確的,但不解決我的問題。我這樣修正:

window.rootViewController = rootView 
     window.makeKeyAndVisible() 

那樣簡單。感謝大家。