使用 Swift 3 在文本字段为空时禁用警报按钮 [英] Disable Alert Button when Text Field is Null with Swift 3

查看:46
本文介绍了使用 Swift 3 在文本字段为空时禁用警报按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当文本框为空时,我想禁用警报按钮.
我把按钮放在表格视图单元格中.因此,当您单击该按钮时,会弹出警告框.
我的代码如下.

I would like to disable the alert button when the text box is null.
I put the button in Table View Cell. So, when you click that button, the alert box will pop up.
My codes are below.

  func cellTapped(cell: DeviceListTableCell) {
    self.showAlertForRow(row: tableView.indexPath(for: cell)!.row)
}

    func showAlertForRow(row: Int) {
    let alert = UIAlertController(
        title: "Enter Password !!!!!",
        message: "",
        preferredStyle: .alert)

    alert.addTextField { (textField) in

    }


    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
        UIAlertAction in

        let pwd = alert.textFields?[0]

        self.password = pwd?.text

        debugPrint(self.password)
        debugPrint("Press OK")


        DispatchQueue.main.async(execute: {
            if(self.password == ""){

                debugPrint("Null Password!")
            }else{
                debugPrint("Not Null Password!")

            }
        })


    }

    alert.addAction(okAction)

    // Present the controller
    DispatchQueue.main.async(execute: {
        self.present(alert, animated: true, completion: nil)
    })

}

protocol ButtonCellDelegate {
func cellTapped(cell: DeviceListTableCell)}

谁能帮我禁用/启用警报按钮?

Could anyone help me how to disable/enable the alert button?

推荐答案

观察 UITextFieldTextDidChange 通知以在文本更改时收到通知,然后启用和禁用 okAction

Observe the UITextFieldTextDidChange notification to be notified when text is changed and then enable and disable okAction

// Create an alert controller
    let alertController = UIAlertController(title: "Alert", message: "Please enter text", preferredStyle: .alert)

    // Create an OK Button
    let okAction = UIAlertAction(title: "OK", style: .default) { (_) in
      // Print "OK Tapped" to the screen when the user taps OK
      print("OK Tapped")
    }

    // Add the OK Button to the Alert Controller
    alertController.addAction(okAction)


    // Add a text field to the alert controller
    alertController.addTextField { (textField) in

      // Observe the UITextFieldTextDidChange notification to be notified in the below block when text is changed
      NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification, object: textField, queue: OperationQueue.main, using:
        {_ in
          // Being in this block means that something fired the UITextFieldTextDidChange notification.

          // Access the textField object from alertController.addTextField(configurationHandler:) above and get the character count of its non whitespace characters
          let textCount = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines).count ?? 0
          let textIsNotEmpty = textCount > 0

          // If the text contains non whitespace characters, enable the OK Button
          okAction.isEnabled = textIsNotEmpty

      })
    }

这篇关于使用 Swift 3 在文本字段为空时禁用警报按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆