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.
答案 0 :(得分:1)
您只需要将UILabel
代码放在“创建” UIAlertAction
块之外,像这样。
此行(在“创建”操作块内)导致问题-> dialogMessage.view.addSubview(label)
我希望这会对您有所帮助。
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)
}
答案 1 :(得分:0)
在验证部分,只需在显示消息之前将标签文本设置为空字符串
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)
}