ios - Swift - 如果语句不能正常工作

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

如有任何帮助,我们将不胜感激。谢谢:)

最佳答案

试试这个:

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

关于ios - Swift - 如果语句不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32875280/

相关文章:

Javascript 如果值为 8,11,18,则将 "a"更改为 "an"

c - if语句与C中的字符串比较

ios - iOS : A server with the specified hostname could not be found 上的 AWS 转录错误

iphone - NSNumber 存储零值而不是正确的数值

swift - 在 Swift 中模拟 CLBeacon 对象

ios - 名称文本字段成为强制性的然后 UIAlert 停止工作

mysql - 将 IF 放入选择查询中

ios - 如何仅在三个边上设置 UITextField 边框/CALayer 边框 ios

iphone - 实现 UITableView 委托(delegate)和数据源 : code reuse 的 UIViewController 的子类化

ios - SWrevealviewcontroller 应该在登录后在欢迎屏幕上可见