[英]Is there a way to call an alert controller from inside a current alert controller (or a textfield in an action sheet)? Xcode 8, Swift 3, IOS
Please help! 请帮忙! Im a huge beginner.
我是一个巨大的初学者。 Im trying to get a picker view and a textfield into one alert controller.
我试图将选择器视图和文本字段集成到一个警报控制器中。 Apparently I can't add a picker view into an alert controller since IOS 8. Instead I need to use an action sheet with multiple actions .
显然,自IOS 8起,我无法将选择器视图添加到警报控制器中。相反,我需要使用包含多个操作的操作表 。 Why can't I use a default alert style?
为什么不能使用默认警报样式? Because I need more than 2 actions and the default alert style apparently only permits a max of 2 actions.
因为我需要两个以上的操作,并且默认的警报样式显然只允许最多两个操作。 So, I need to use an action sheet.
因此,我需要使用操作表。 Only problem is I can't seem to find a way to put an editable textfield into an action sheet, to have multiple action options - instead of an editable textfield in my default alert style, with only two action options.
唯一的问题是,我似乎找不到一种将可编辑文本字段放入操作表中,具有多个操作选项的方法-而不是默认警报样式中只有两个操作选项的可编辑文本字段 。
This is what I have so far, for my editable textfield in my default alert style, with only two options (OK and cancel): 这是到目前为止,对于默认警报样式下的可编辑文本字段,只有两个选项(确定和取消):
@IBOutlet weak var TEXTTESTLabel: UILabel!
@IBAction func TEXTESTTapped(_ sender: UIButton) {
print("TEXTTEST Button Tapped")
openTEXTTESTAlert()
}
func openTEXTTESTAlert() {
//Create Alert Controller
let alert9 = UIAlertController (title: "Test Your Text:", 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.TEXTTESTLabel.text = textfield?.text!
}
alert9.addAction(ok9)
//Add Text Field
alert9.addTextField { (textfield: UITextField) in
textfield.placeholder = "TEXTTEST"
}
//Present Alert Controller
self.present(alert9, animated:true, completion: nil)
}
////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////
Since this, I've gotten help to create two separate alert controllers but it looks and feels messy. 从那时起,我得到了创建两个单独的警报控制器的帮助,但是它看起来和感觉都很混乱。 It's set up so if I click button 2 the default alert style with a textfield will pop and insert text into a label.
设置好后,如果我单击按钮2 ,则会弹出带有文本字段的默认警报样式,并将文本插入标签中。 But, if i click button 1, AND THEN I click button 2, it will show an action sheet with 4 actions that put a word into that same label.
但是,如果我单击按钮1,然后再单击按钮2,它将显示一个包含4个操作的操作表,这些操作将一个单词放入同一标签。 This is what I have for that method:
这就是我要的那种方法:
@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 / 4.0, width: 1.0, height: 1.0)
self.present(alert9, animated:true, completion: nil)
}
I find that this method is confusing to the application user. 我发现此方法使应用程序用户感到困惑。 If anyone has a different method to get a textfield into an action sheet (or even a separate alert controller imbedded in a current alert controller), I'd appreciate it VERY much!
如果有人用不同的方法将文本字段放入操作表中(或什至是嵌入当前警报控制器中的单独警报控制器),我将非常感激! Thank you :)
谢谢 :)
For Action sheet you can use as 对于操作表,您可以将其用作
func showActionSheet()
{
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
actionSheet.addAction(UIAlertAction(title: "1:", style: UIAlertActionStyle.default, handler: { (alert:UIAlertAction!) -> Void in
)
self.action1()
}))
actionSheet.addAction(UIAlertAction(title: "2", style: UIAlertActionStyle.default, handler: { (alert:UIAlertAction!) -> Void in
self.action2()
}))
actionSheet.addAction(UIAlertAction(title: "3", style: UIAlertActionStyle.default, handler: { (alert:UIAlertAction!) -> Void in
// self.action3()
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
// showActionSheet()
I found the solution! 我找到了解决方案! I was wrong about having only 2 actions in the default alert style.
我错在默认警报样式中只有2个操作。 How to scroll through actions in alert controller?
如何在警报控制器中滚动操作? Xcode 8, Swift 3, IOS
Xcode 8,Swift 3,IOS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.