gpt4 book ai didi

ios - 带有下拉菜单和键盘选项的警报 Controller ? swift 3、Xcode 8、IOS

转载 作者:行者123 更新时间:2023-11-29 00:29:00 26 4
gpt4 key购买 nike

在编程方面,我是一个巨大的初学者。

我目前设置了一个警报 Controller ,因此当我单击 View Controller 中的按钮时 an alert controller pops up .我可以在文本字段中输入文本(在警报 Controller 中),当我单击“确定”(在警报 Controller 中)时,文本将显示在标签上(在我的 View Controller 中)。这是我所拥有的代码:

//Text button

@IBOutlet weak var TextLabel: UILabel!

@IBAction func TextButtonTapped(_ sender: UIButton) {
print("Text Button Tapped")
openTextAlert()
}

func openTextAlert() {
//Create Alert Controller
let alert9 = UIAlertController (title: "Whatever Text Your Heart Desires:", message: nil, preferredStyle: UIAlertControllerStyle.alert)

//Create Cancel Action
let cancel9 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)

alert9.addAction(cancel9)

//Create OK Action
let ok9 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK")
let textfield = alert9.textFields?[0]
print(textfield?.text!)
self.TextLabel.text = textfield?.text!
}

alert9.addAction(ok9)

//Add Text Field
alert9.addTextField { (textfield: UITextField) in
textfield.placeholder = "Whatever text you want to enter"
}

//Present Alert Controller
self.present(alert9, animated:true, completion: nil)
}

如何在我的警报 Controller 中执行多个操作,以便它也充当 a dropdown menu with certain words so that I don't need to always write out key words?我既需要能够输入自己的单词和短语,也需要预设单词和短语以提高效率。

求助!我是个大菜鸟。谢谢 :)Swift 3、Xcode 8、IOS

最佳答案

只需使用您的代码编写即可。这是您需要的吗?

编辑:yourNewButtonTapped 函数只是向您展示如何设置条件来显示简单警报或操作表。在此功能中,当您单击新按钮,然后单击 TextButton 时,将显示您的警报。如果您再次单击新按钮,则会将 userWantsToShowAlert 设置为 false。当您再次单击 textButtonTapped 时,将调用 openActionSheet。

您可以通过多种方式做到这一点。

@IBOutlet weak var TextLabel: UILabel!

var userWantsToShowAlert = false

@IBAction func yourNewButtonTapped(_ sender: UIButton) {
userWantsToShowAlert = !userWantsToShowAlert
print("User wants to show alert? \(userWantsToShowAlert)")
//This is userWantsToShowAlert is false, it will change it to true. And if it is true, it will change it to false.
}

@IBAction func TextButtonTapped(_ sender: UIButton) {
print("Text Button Tapped")
if(userWantsToShowAlert){
openTextAlert()
}else{
openActionSheetAlert()
}


}

func openTextAlert() {
//Create Alert Controller
let alert9 = UIAlertController (title: "Whatever Text Your Heart Desires:", message: nil, preferredStyle: UIAlertControllerStyle.alert)

//Create Cancel Action
let cancel9 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)

alert9.addAction(cancel9)

//Create OK Action
let ok9 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK")
let textfield = alert9.textFields?[0]
print(textfield?.text!)
self.TextLabel.text = textfield?.text!
}

alert9.addAction(ok9)

//Add Text Field
alert9.addTextField { (textfield: UITextField) in
textfield.placeholder = "Whatever text you want to enter"
}

//Present Alert Controller
self.present(alert9, animated:true, completion: nil)
}

func openActionSheetAlert(){
let alert9 = UIAlertController (title: "Whatever Text Your Heart Desires:", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)

//Create Cancel Action
let cancel9 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
alert9.addAction(cancel9)


let bt1 = UIAlertAction(title: "1", style: UIAlertActionStyle.default){
(action) in self.TextLabel.text = "Word 1"}

alert9.addAction(bt1)

let bt2 = UIAlertAction(title: "2", style: UIAlertActionStyle.default){
(action) in self.TextLabel.text = "Word 2"}

alert9.addAction(bt2)


let bt3 = UIAlertAction(title: "3", style: UIAlertActionStyle.default){
(action) in self.TextLabel.text = "Word 3"}

alert9.addAction(bt3)

let bt4 = UIAlertAction(title: "4", style: UIAlertActionStyle.default){
(action) in self.TextLabel.text = "Word 4"}
alert9.addAction(bt4)

alert9.popoverPresentationController?.sourceView = self.view
alert9.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.size.width / 2.0, y: self.view.bounds.size.height / 2.0, width: 1.0, height: 1.0)
self.present(alert9, animated:true, completion: nil)
}

关于ios - 带有下拉菜单和键盘选项的警报 Controller ? swift 3、Xcode 8、IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42369519/

26 4 0