助けてください!私は巨大な初心者です。私はpicker viewとa textfield into one alert controller.を取得しようとしています明らかにIOS 8以来、警告コントローラにピッカービューを追加することはできません。代わりにaction sheet with multiple actionsを使用する必要があります。なぜ私はdefault alert style?を使用できないのですか?2つ以上のアクションが必要なので、デフォルトのアラートスタイルでは明らかに最大2つのアクションしか許可されません。だから、私はアクションシートを使う必要があります。唯一の問題は、editable textfieldにan action sheet,に複数のアクションオプション - an editable textfield in my default alert style,の代わりに2つのアクションオプションを指定する方法を見つけることができないようです。現在のアラートコントローラ(またはアクションシートのテキストフィールド)の内部からアラートコントローラを呼び出す方法はありますか? Xcode 8、Swift 3、IOS
これは私が私の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)
}
のために、これまで持っているものである//////////////////////////// ///////////////////////////////////////////////////////////////// ////
これ以降、2つの別々のアラートコントローラを作成するのに役立っていますが、見た目が乱雑です。これは、ボタン2 the default alert style with a textfield will popをクリックし、テキストをラベルに挿入するように設定されています。しかし、ボタン1をクリックしてボタン2をクリックすると、同じラベルに単語を入れたan action sheet with 4 actionsが表示されます。これは私がその方法で持っているものです:
@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)
}
私はこの方法がアプリケーションユーザーにとって混乱していることがわかります。誰かがアクションシート(または現在のアラートコントローラに埋め込まれた別のアラートコントローラ)にテキストフィールドを取得する別の方法を持っているなら、それは非常に感謝しています! ありがとうございました:)
コードが動作するようです – DanielG