Reputation: 294
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. 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.
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.
Upvotes: 4
Views: 4853
Reputation: 4552
you just need to put UILabel
code outside of "Create" UIAlertAction
block like this.
This line (inside the Create action block) causes the issue --> 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)
}
Upvotes: 3
Reputation: 141
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)
}
Upvotes: 0