Swift - 如果声明不能正常工作

时间:2015-09-30 20:43:47

标签: ios swift if-statement uibutton

一旦两个文本字段输入了文本,我就试图为UIButton设置动画。即使没有输入文本,该按钮也会动画。我想也许这是一个存在占位符文本的问题,但这没有任何影响。我试过了!=""以及!= nil

这是我的代码

    override func viewDidLoad() {
    super.viewDidLoad()

    self.title = ""

    textFieldConfig()
    loginButtonConfig("LOG IN")
}

func loginButtonConfig(title:String) {
    let frameWidth = self.view.frame.width
    let frameHeight = self.view.frame.height

    var button = UIButton()
    button.frame = CGRect(x: 0, y: 300, width: 0, height: 50)
    button.bounds = CGRect(x: 0, y: 0, width: 0, height: 50)
    button.backgroundColor = UIColor(red: 24.0/255.0, green: 198.0/255.0, blue: 152.0/255.0, alpha: 1.0)
    button.setTitle(title, forState: .Normal)
    button.titleLabel?.textAlignment = NSTextAlignment.Center
    self.view.addSubview(button)

    //WHY ISN'T THE IF STATEMENT WORKING AS IT SHOULD???
    if self.userTextField.text != nil && self.passwordTextField.text != nil {
       UIView.animateWithDuration(1.0, animations: { () -> Void in
        button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
        button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
       })
    }
}

任何帮助将不胜感激。谢谢:))

2 个答案:

答案 0 :(得分:1)

试试这个:

if !self.userTextField.text.isEmpty && !self.passwordTextField.isEmpty {
   UIView.animateWithDuration(1.0, animations: { () -> Void in
    button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
    button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
   })
}

答案 1 :(得分:0)

试试这个:

if self.userTextField.text! != "" && self.passwordTextField.text! != "" {
   UIView.animateWithDuration(1.0, animations: { () -> Void in
    button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
    button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
   })
}