swift ios 中带有文本字段验证警报显示问题的 UIAlertController
UIAlertController with textfield validation alert displaying issue in swift ios
我能够显示带有文本字段的警报控制器并正确获取文本字段输入数据。
这里我想根据文本字段数据进行两次验证。 1. 如果文本字段中没有文本并点击创建显示,请在标签中输入房间名称警报。 2. 如果输入的文本与现有字符串匹配,然后创建点击显示您已经使用此名称创建了房间,这些屏幕截图如下所示。
这里的问题是,如果我先不显示文本警报,然后再显示第二个匹配警报,两者将合并并显示在如下所示的警报中。我不想同时显示两者。
下面是我显示警报控制器的完整代码。
@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
}
}
有人可以建议我我无法处理标签中显示的这两种情况错误文本。提前致谢。
在您的验证部分,只需在显示您的消息之前将标签文本设置为空字符串
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)
}
你只需要像这样将 UILabel
代码放在 "Create" 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. 如果文本字段中没有文本并点击创建显示,请在标签中输入房间名称警报。 2. 如果输入的文本与现有字符串匹配,然后创建点击显示您已经使用此名称创建了房间,这些屏幕截图如下所示。
这里的问题是,如果我先不显示文本警报,然后再显示第二个匹配警报,两者将合并并显示在如下所示的警报中。我不想同时显示两者。
下面是我显示警报控制器的完整代码。
@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
}
}
有人可以建议我我无法处理标签中显示的这两种情况错误文本。提前致谢。
在您的验证部分,只需在显示您的消息之前将标签文本设置为空字符串
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)
}
你只需要像这样将 UILabel
代码放在 "Create" 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)
}