swift2 是否有一种方法可以使用swift在alertview内的文本字段中提供验证

9wbgstp7  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(166)

我有一个alertview,它显示了一个文本字段,用户必须输入它作为强制。问题是如果用户点击“授权”没有输入它的alertview被关闭。我不能想出一个方法来显示给用户,它是强制的,而不关闭alertview。

编码:
函数表视图(表视图:UITableView!,didSelectRowAt索引路径索引路径:NSIndexPath!){

print("You selected cell #\(self.empNameArr[indexPath.row])!")
    let alertController = UIAlertController(title: "OD Authorise", message: "", preferredStyle: UIAlertControllerStyle.Alert)

    let AUTHORISE = UIAlertAction(title: "AUTHORISE", style: UIAlertActionStyle.Default, handler: {
        alert -> Void in

        let firstTextField = alertController.textFields![3] as UITextField
        print("<><><><><><>",firstTextField.text)

    })

    let DENY = UIAlertAction(title: "DENY", style: UIAlertActionStyle.Default, handler: {
        (action : UIAlertAction!) -> Void in

    })

    let CANCEL = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.Default, handler: {
        (action : UIAlertAction!) -> Void in

    })

    alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in
        txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11)
        txtRemarks.text = " Employee Name :\(self.empNameArr[indexPath.row]) "
        txtRemarks.userInteractionEnabled=false
        txtRemarks.borderStyle = UITextBorderStyle.None

    }

    alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in
        txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11)
        txtRemarks.text = " From Date :\(self.leavDateArr[indexPath.row]) "
        txtRemarks.userInteractionEnabled=false
        txtRemarks.borderStyle = UITextBorderStyle.None

    }
    alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in
        txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11)
        txtRemarks.text = " To Date :\(self.ToDate[indexPath.row]) "
        txtRemarks.userInteractionEnabled=false
        txtRemarks.borderStyle = UITextBorderStyle.None

    }

    alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in
        txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11)
        txtRemarks.text = " Leave reason :\(self.Reason[indexPath.row]) "
        txtRemarks.userInteractionEnabled=false
        txtRemarks.borderStyle = UITextBorderStyle.None

    }

    alertController.addTextFieldWithConfigurationHandler { (txtRemarks : UITextField!) -> Void in
        txtRemarks.placeholder = "Enter Your Remarks"
        txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 15)
        txtRemarks.userInteractionEnabled=true
        txtRemarks.borderStyle = UITextBorderStyle.Line
        txtRemarks.textAlignment = NSTextAlignment.Center

    }

    alertController.addAction(AUTHORISE)
    alertController.addAction(DENY)
    alertController.addAction(CANCEL)

    self.presentViewController(alertController, animated: true, completion: nil)

}
e4yzc0pl

e4yzc0pl1#

将类设置为UIAlertViewDelegate,并将alrtView的委托设置为类。
那就这样做

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
    NSString *name = [alertView textFieldAtIndex:0].text;
    // Here check for validation. If the text is empty disable button or however you would like to handle it
}
}
pxyaymoc

pxyaymoc2#

class ViewController: UIViewController,UITextFieldDelegate
    {

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        var authorizeaction:UIAlertAction?

        @IBAction func tapBtnaction(sender: AnyObject)
        {
            let titleStr = "title"
            let messageStr = "message"

            let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert)

            let placeholderStr =  "Enter your Remarks"

            alert.addTextFieldWithConfigurationHandler({(textField: UITextField) in
                textField.placeholder = placeholderStr
                textField.addTarget(self, action: #selector(self.textChanged(_:)), forControlEvents: .EditingChanged)
            })

            let authorize = UIAlertAction(title: "Authorize", style: UIAlertActionStyle.Default, handler: { (_) -> Void in

            })

            let deny = UIAlertAction(title: "Deny", style: UIAlertActionStyle.Default, handler: { (_) -> Void in
                let textfield = alert.textFields!.first!

                //Do what you want with the textfield!
            })
            let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (_) -> Void in
                let textfield = alert.textFields!.first!

                //Do what you want with the textfield!
            })
            alert.addAction(cancel)
            alert.addAction(authorize)
            alert.addAction(deny)

            //self.actionToEnable = action
            authorizeaction = authorize
            authorizeaction!.enabled = false
            self.presentViewController(alert, animated: true, completion: nil)

        }
        func textChanged(sender:UITextField)
        {

           if sender.text?.characters.count > 0
        {
        authorizeaction?.enabled = true
        }
        else
        {
            authorizeaction?.enabled = false
        }

        }

    }
Output:

编辑:

如果您不想启用或禁用Authorize Action,则可以使用以下代码。

let authorize = UIAlertAction(title: "Authorize", style: UIAlertActionStyle.Default, handler: { (_) -> Void in
            if let textfield : UITextField = alert.textFields![0]
            {
                if textfield.text?.characters.count == 0 {
                    //empty

                    self.presentViewController(alert, animated: true, completion: { 
                        let tempAlert = UIAlertController(title: "Error", message: "Please Enter remarks", preferredStyle: UIAlertControllerStyle.Alert)
                        tempAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: { (action) in

                        }))

                        alert.presentViewController(tempAlert, animated: true, completion: nil)
                    })

                } else {
                    //authorize
                }
            }

        })

另一个仅显示消息的选项是吐司。https://github.com/scalessec/Toast

piah890a

piah890a3#

您需要将手势识别器添加到UIAlertController中,以便在消除发生之前触发,您可以参考以下答案:Prevent UIAlertController to dismiss
另外,您可以使用以下代码:

let alertController = UIAlertController(title: "OD Authorise", message: "", preferredStyle: UIAlertControllerStyle.alert)

    let AUTHORISE = UIAlertAction(title: "AUTHORISE", style: UIAlertActionStyle.default, handler: {
        alert -> Void in

        let firstTextField = alertController.textFields![4] as UITextField
        if firstTextField.text?.count == 0
        {
            //print("Please enter your remark first")
            SVProgressHUD.showError(withStatus: "Please enter your remark first")
            self.present(alertController, animated: true, completion: nil)

            return
        }
        print("<><><><><><>",firstTextField.text ?? "")

    })

    let DENY = UIAlertAction(title: "DENY", style: UIAlertActionStyle.default, handler: {
        (action : UIAlertAction!) -> Void in

    })

    let CANCEL = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.default, handler: {
        (action : UIAlertAction!) -> Void in

    })

    alertController.addTextField { (txtRemarks : UITextField!) -> Void in
        txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11)
        txtRemarks.text = " Employee Name : YOUR ARRAY OBJECT"//\(self.empNameArr[indexPath.row]) "
        txtRemarks.isUserInteractionEnabled=false
        txtRemarks.borderStyle = UITextBorderStyle.none

    }

    alertController.addTextField { (txtRemarks : UITextField!) -> Void in
        txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11)
        txtRemarks.text = " From Date : YOUR ARRAY OBJECT"//\(self.leavDateArr[indexPath.row]) "
        txtRemarks.isUserInteractionEnabled=false
        txtRemarks.borderStyle = UITextBorderStyle.none

    }
    alertController.addTextField { (txtRemarks : UITextField!) -> Void in
        txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11)
        txtRemarks.text = " To Date : YOUR ARRAY OBJECT"//\(self.ToDate[indexPath.row]) "
        txtRemarks.isUserInteractionEnabled=false
        txtRemarks.borderStyle = UITextBorderStyle.none

    }

    alertController.addTextField { (txtRemarks : UITextField!) -> Void in
        txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 11)
        txtRemarks.text = " Leave reason : YOUR ARRAY OBJECT"//\(self.Reason[indexPath.row]) "
        txtRemarks.isUserInteractionEnabled=false
        txtRemarks.borderStyle = UITextBorderStyle.none

    }

    alertController.addTextField { (txtRemarks : UITextField!) -> Void in
        txtRemarks.placeholder = "Enter Your Remarks"
        txtRemarks.font = UIFont(name: (txtRemarks.font?.fontName)!, size: 15)
        txtRemarks.isUserInteractionEnabled=true
        txtRemarks.borderStyle = UITextBorderStyle.line
        txtRemarks.textAlignment = NSTextAlignment.center

    }

    alertController.addAction(AUTHORISE)
    alertController.addAction(DENY)
    alertController.addAction(CANCEL)

    self.present(alertController, animated: true, completion: nil)

相关问题