gpt4 book ai didi

ios - swift/Xcode 中的 UIAlertView

转载 作者:行者123 更新时间:2023-11-30 13:11:37 25 4
gpt4 key购买 nike

我有一个关于 swift 中的 UIAlertview 的问题。我有一个警报 View ,允许用户在“教师”和“学生”两个选项之间进行选择,并根据单击的按钮将用户带到新屏幕。它工作正常,唯一的问题是我在每个新屏幕上都有一个后退按钮,如果用户单击了错误的按钮,该按钮会将用户带回到上一个屏幕。这就是问题发生的地方。如果用户在 naviagting 返回页面后单击触发 alertview 的按钮,则 alertivew 会记住用户上次单击的按钮并执行以此为基础。抱歉,如果这令人困惑,我希望他们每次都能在选项之间进行选择,并且让 alertview 不根据他们之前的选择执行。抱歉,如果这令人困惑,非常感谢所有帮助。

这是我目前的代码

@IBAction func needToRegisterClicked(sender: AnyObject) {

    let alertController = UIAlertController(title: "Please choose", message: "Teacher or Student?", preferredStyle: UIAlertControllerStyle.Alert)

alertController.view.tintColor = UIColor.blackColor()

alertController.addAction(UIAlertAction(title: "Teacher", style: .Default, handler: { (action) in

print("Teacher Chosen")



let ac = UIAlertController(title: "Enter Access Code", message: nil, preferredStyle: .Alert)
ac.addTextFieldWithConfigurationHandler { (textField: UITextField!) in
textField.keyboardType = UIKeyboardType.NumberPad }



ac.view.tintColor = UIColor.blackColor()

let submitAction = UIAlertAction(title: "Submit", style: .Default) { [unowned self, ac] (action: UIAlertAction!) in
let answer = ac.textFields![0] as! UITextField
if answer.text == "3280464" {

self.performSegueWithIdentifier("School Register", sender: nil)

} else {

let alert2 = UIAlertController(title: "Sorry!", message: "Incorrect access code", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert2, animated: true, completion: nil)

alert2.addAction(UIAlertAction(title: "Dismiss", style: .Default, handler: nil))


}
// do something interesting with "answer" here
}

ac.addAction(submitAction)

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






}))

alertController.addAction(UIAlertAction(title: "Student", style: .Default, handler: { (action) in

print("Student Chosen")

self.performSegueWithIdentifier("Student", sender: nil)


}))
self.dismissViewControllerAnimated(true, completion: nil)

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





}

编辑:每次按下按钮时,我究竟如何实例化一个新的alertView?抱歉,如果问这个问题很愚蠢,我对整个事情非常陌生。

编辑:我尝试了所有建议的选项,但没有成功,但我现在能够自己解决。我所需要做的就是从末尾删除 self.dismissViewControllerAnimated(true,completion: nil) 。感谢所有回复的人,我真诚地感谢您抽出时间。

最佳答案

我认为如果用户在警报 Controller 中按下了不正确的操作,您想要的是返回到第一个 ViewController,所以我所做的是:- 我添加了“学生”按钮,其代码与教师按钮相同- 我创建了一个存储 bool 值的var,如果true是老师,否则是学生,当你选择你是老师时,var存储一个true值,否则,如果按下学生按钮,它存储false,然后转到下一个ViewController- 在第二个 ViewController 中我创建了一个按钮(后退)

self.performSegueWithIdentifier("backToFirst", 发件人: self)

FirstViewController

import UIKit

var isTeacher: bool !

ViewController 类:UIViewController {

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

@IBAction func needToRegisterClicked(sender: AnyObject) {

let alertController = UIAlertController(title: "Please choose", message: "Teacher or Student?", preferredStyle: UIAlertControllerStyle.Alert)

alertController.view.tintColor = UIColor.blackColor()

//Button teacher

alertController.addAction(UIAlertAction(title: "Teacher", style: .Default, handler: { (action) in

print("Teacher Chosen")

isTeacher = true

let ac = UIAlertController(title: "Enter Access Code", message: nil, preferredStyle: .Alert)
ac.addTextFieldWithConfigurationHandler { (textField: UITextField!) in
textField.keyboardType = UIKeyboardType.NumberPad }



ac.view.tintColor = UIColor.blackColor()

let submitAction = UIAlertAction(title: "Submit", style: .Default) { [unowned self, ac] (action: UIAlertAction!) in
let answer = ac.textFields![0] as! UITextField
if answer.text == "3280464" {

self.performSegueWithIdentifier("School Register", sender: self)

} else {

let alert2 = UIAlertController(title: "Sorry!", message: "Incorrect access code", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert2, animated: true, completion: nil)

alert2.addAction(UIAlertAction(title: "Dismiss", style: .Default, handler: nil))


}
// do something interesting with "answer" here
}

ac.addAction(submitAction)

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

}
//Ends Button Teacher

)


)

//Button Student

alertController.addAction(UIAlertAction(title: "Student", style: .Default, handler: { (action) in

print("Student Chosen")

isTeacher = false

let ac = UIAlertController(title: "Enter Access Code", message: nil, preferredStyle: .Alert)
ac.addTextFieldWithConfigurationHandler { (textField: UITextField!) in
textField.keyboardType = UIKeyboardType.NumberPad }



ac.view.tintColor = UIColor.blackColor()

let submitAction = UIAlertAction(title: "Submit", style: .Default) { [unowned self, ac] (action: UIAlertAction!) in
let answer = ac.textFields![0] as! UITextField
if answer.text == "3280464" {

self.performSegueWithIdentifier("School Register", sender: self)

} else {

let alert2 = UIAlertController(title: "Sorry!", message: "Incorrect access code", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert2, animated: true, completion: nil)

alert2.addAction(UIAlertAction(title: "Dismiss", style: .Default, handler: nil))


}
// do something interesting with "answer" here
}

ac.addAction(submitAction)

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

}


)


)

//Ends Button Student

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

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

}

SecondViewController

import UIKit

记录的类:ViewController{

@IBAction func Back(sender: AnyObject) {

self.performSegueWithIdentifier("backToFirst", sender: self)

}

@IBOutlet weak var navBar: UINavigationBar!

override func viewDidLoad() {
super.viewDidLoad()

if(isTeacher == true){

navBar.topItem?.title = "Teacher Area"

}else{

navBar.topItem?.title = "Student Area"

}



}

}

希望对你有帮助

关于ios - swift/Xcode 中的 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38665827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com