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

问题描述 投票:0回答:2

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

这是我的代码

    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)
       })
    }
}

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

ios swift if-statement uibutton
2个回答
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)
   })
}

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)
   })
}
© www.soinside.com 2019 - 2024. All rights reserved.