简体   繁体   English

带有文本字段验证警报的UIAlertController在Swift iOS中显示问题

[英]UIAlertController with textfield validation alert displaying issue in swift ios

I'm able to display alert controller with textfield and get textfield input data properly. 我能够显示带有文本字段的警报控制器,并正确获取文本字段输入数据。

Here i wanted to do two validation based on textfield data. 在这里,我想基于文本字段数据进行两次验证。 1. if no text in textfield and tapped on create display please enter room name alert in label. 1.如果文本字段中没有文本并且没有在创建显示中点击,请在标签中输入房间名称警报。 2. if entered text is matches to already available string and then create tapped display you've already created room with this name these screenshots are shown in below. 2.如果输入的文本与现有字符串匹配,然后创建点击显示,则您已经使用此名称创建了房间,这些屏幕截图如下所示。

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

Here the issue is if i display no text alert first and then second matches alert both are combined and showing in alert shown in below. 这里的问题是,如果我先不显示任何文本警报,然后再将第二个匹配警报组合在一起,并显示在下面所示的警报中。 i don't want to display both in one time. 我不想一次显示两个。

在此处输入图片说明

Here is my complete code to display the alert controller below. 这是我完整的代码,用于在下面显示警报控制器。

    @IBAction func getAlertBtn(_ sender: Any) {
    alertControllerWithTf()
}
var roomTextField: UITextField!
func alertControllerWithTf(){
    let dialogMessage = UIAlertController(title: "New Room", message: nil, preferredStyle: .alert)
    let Create = UIAlertAction(title: "Create", style: .default, handler: { (action) -> Void in
        if let userInput = self.roomTextField!.text {
            let label = UILabel(frame: CGRect(x: 0, y: 40, width: 270, height:18))
            label.textAlignment = .center
            label.textColor = .red
            label.font = label.font.withSize(12)
            dialogMessage.view.addSubview(label)
            label.isHidden = true
            if userInput == ""{
                label.text = "Please enter room name to create."
                label.isHidden = false
                self.present(dialogMessage, animated: true, completion: nil)

            }else if self.haveSameRoomName(createdRoomName: userInput){
                label.text = "You've already created room with this name."
                label.isHidden = false
                self.present(dialogMessage, animated: true, completion: nil)
            }else{
                print("Create button success block called do stuff here....")
            }
        }
    })
    let cancel = UIAlertAction(title: "Cancel", style: .default) { (action) -> Void in
        print("Cancel button tapped")
    }

    //Add OK and Cancel button to dialog message

    dialogMessage.addAction(Create)
    dialogMessage.addAction(cancel)
    // Add Input TextField to dialog message
    dialogMessage.addTextField { (textField) -> Void in
        self.roomTextField = textField
        self.roomTextField?.placeholder = "Please enter room name"
    }
    // Present dialog message to user
    self.present(dialogMessage, animated: true, completion: nil)
}
func haveSameRoomName(createdRoomName: String) -> Bool{
    let allRoomNames =  ["FIRST", "SECOND", "THIRD", "FOURTH", "FIFTH","SIXTH"]
    if allRoomNames.contains(createdRoomName){
        return true
    }else{
        return false
    }
}

Can somebody please suggest me i can't able to handle these two cases error text displaying in label. 有人可以建议我,我无法处理标签中显示的这两种情况的错误文本。 thanks in advance. 提前致谢。

you just need to put UILabel code outside of "Create" UIAlertAction block like this. 您只需要将UILabel代码放在“创建” UIAlertAction块之外,就像这样。

This line (inside the Create action block) causes the issue --> dialogMessage.view.addSubview(label) 此行(在“创建”操作块内)导致问题-> dialogMessage.view.addSubview(label)

I hope this will help you. 我希望这能帮到您。

func alertControllerWithTf() {
    let dialogMessage = UIAlertController(title: "New Room", message: nil, preferredStyle: .alert)
    let label = UILabel(frame: CGRect(x: 0, y: 40, width: 270, height:18))
    label.textAlignment = .center
    label.textColor = .red
    label.font = label.font.withSize(12)
    dialogMessage.view.addSubview(label)
    label.isHidden = true

    let Create = UIAlertAction(title: "Create", style: .default, handler: { (action) -> Void in
        if let userInput = self.roomTextField!.text {
            if userInput == "" {
                label.text = ""
                label.text = "Please enter room name to create."
                label.isHidden = false
                self.present(dialogMessage, animated: true, completion: nil)

            }
            else if self.haveSameRoomName(createdRoomName: userInput){
                label.text = ""
                label.text = "You've already created room with this name."
                label.isHidden = false
                self.present(dialogMessage, animated: true, completion: nil)
            }
            else{
                print("Create button success block called do stuff here....")
            }
        }
    })
    let cancel = UIAlertAction(title: "Cancel", style: .default) { (action) -> Void in
        print("Cancel button tapped")
    }

    //Add OK and Cancel button to dialog message

    dialogMessage.addAction(Create)
    dialogMessage.addAction(cancel)
    // Add Input TextField to dialog message
    dialogMessage.addTextField { (textField) -> Void in
        self.roomTextField = textField
        self.roomTextField?.placeholder = "Please enter room name"
    }

    // Present dialog message to user
    self.present(dialogMessage, animated: true, completion: nil)
}

In your validation part just set the label text to empty string before displaying your message 在验证部分中,只需在显示消息之前将标签文本设置为空字符串即可

if userInput == "" {
    label.text = "" *// Put this in your code*
    label.text = "Please enter room name to create."
    label.isHidden = false
    self.present(dialogMessage, animated: true, completion: nil)

     } else if self.haveSameRoomName(createdRoomName: userInput){
         label.text = ""  *// Put this in your code*
         label.text = "You've already created room with this name."
         label.isHidden = false
         self.present(dialogMessage, animated: true, completion: nil)
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM