如何在Swift中将TextField添加到UIAlertController

Qui*_*don 83 uitextfield ios swift uialertcontroller

我试图UIAlertController用a表示UITextView.当我添加行:

    //Add text field
    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
    }        
Run Code Online (Sandbox Code Playgroud)

我收到一个Runtime错误:

致命错误:在展开Optional值时意外发现nil

    let alertController: UIAlertController = UIAlertController(title: "Find image", message: "Search for image", preferredStyle: .Alert)

    //cancel button
    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
        //cancel code
    }
    alertController.addAction(cancelAction)

    //Create an optional action
    let nextAction: UIAlertAction = UIAlertAction(title: "Search", style: .Default) { action -> Void in
        let text = (alertController.textFields?.first as! UITextField).text
        println("You entered \(text)")
    }
    alertController.addAction(nextAction)

    //Add text field
    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
        textField.textColor = UIColor.greenColor()
    }
    //Present the AlertController
    presentViewController(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

这是ViewController通过我的内容呈现的IBAction.

我从这里下载了代码,它工作正常.我将该方法复制并粘贴到我的代码中,然后中断了.最后一行的自我存在没有影响.

Him*_*jan 168

斯威夫特4

alert.addTextField(configurationHandler: { (textField) in
    textField.placeholder = "Enter First Name"
})
Run Code Online (Sandbox Code Playgroud)

使用此代码,我在我的应用程序中成功运行此代码.

@IBAction func addButtonClicked(sender : AnyObject){
    let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
        textField.placeholder = "Enter Second Name"
    }
    let saveAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: { alert -> Void in
        let firstTextField = alertController.textFields![0] as UITextField
        let secondTextField = alertController.textFields![1] as UITextField
    })
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: {
        (action : UIAlertAction!) -> Void in })
    alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
        textField.placeholder = "Enter First Name"
    }

    alertController.addAction(saveAction)
    alertController.addAction(cancelAction)

    self.presentViewController(alertController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

编辑:Swift 3.0版

@IBAction func addButtonClicked(_ sender: UIButton){
    let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: .alert)
    alertController.addTextField { (textField : UITextField!) -> Void in
        textField.placeholder = "Enter Second Name"
    }
    let saveAction = UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in
        let firstTextField = alertController.textFields![0] as UITextField
        let secondTextField = alertController.textFields![1] as UITextField
        print("firstName \(firstTextField.text), secondName \(secondTextField.text)")
    })
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: { (action : UIAlertAction!) -> Void in })
    alertController.addTextField { (textField : UITextField!) -> Void in
        textField.placeholder = "Enter First Name"
    }

    alertController.addAction(saveAction)
    alertController.addAction(cancelAction)

    self.present(alertController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)


Mea*_* S. 22

在Swift 3.0中添加文本字段:

let alertController = UIAlertController(title: "Title", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in
    let textField = alertController.textFields![0] as UITextField
    // do something with textField
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
    textField.placeholder = "Search"
})   
self.present(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)


Üma*_*mån 13

解决方案:

斯威夫特 4.2

尝试以下几行,看看它是否有效:

let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: .alert)

alertController.addTextField { (textField : UITextField!) -> Void in
    textField.placeholder = "Enter Second Name"
}

let saveAction = UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in
    let firstTextField = alertController.textFields![0] as UITextField
    let secondTextField = alertController.textFields![1] as UITextField
})

let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil )

alertController.addTextField { (textField : UITextField!) -> Void in
    textField.placeholder = "Enter First Name"
}

alertController.addAction(saveAction)
alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。


Qui*_*don 11

所以我开始检查我的代码可能与工作代码有什么不同.我注意到我的ViewController扩展了

UITextFieldDelegate
Run Code Online (Sandbox Code Playgroud)

这显然意味着我需要设置任何子UITextView的委托:

alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
        searchTextField = textField
        searchTextField?.delegate = self //REQUIRED
        searchTextField?.placeholder = "Enter your search terms"
}
Run Code Online (Sandbox Code Playgroud)


Mai*_*jat 7

如何将 textField 添加到 AlertView?让我们保持简短。

这适用于 Swift 3.0 及更高版本。

var nameField: UITextField?
let alertController = UIAlertController(title: "Add Number", message: nil, preferredStyle: .alert)
// Add textfield to alert view
alertController.addTextField { (textField) in
    nameField = textField
}
Run Code Online (Sandbox Code Playgroud)

首先,您实例化一个对象,UIAlertController然后通过访问类的addTextField成员向其添加文本字段UIAlertController


Abh*_*ore 5

UIAlertController最新UITextfieldApple Swift 版本 5.1.3

在您的“添加”中创建一个变量lazy “显示操作警报” UIAlertControllerUIViewControllerUITextFieldDelegateUIButton

class  YourViewController: UIViewController,UITextFieldDelegate {

    //Create Alert Controller Object here
    lazy var alertEmailAddEditView:UIAlertController = {

        let alert = UIAlertController(title:"My App", message: "Customize Add/Edit Email Pop Up", preferredStyle:UIAlertController.Style.alert)

        //ADD TEXT FIELD (YOU CAN ADD MULTIPLE TEXTFILED AS WELL)
        alert.addTextField { (textField : UITextField!) in
            textField.placeholder = "Enter  emails"
            textField.delegate = self
        }

        //SAVE BUTTON
        let save = UIAlertAction(title: "Save", style: UIAlertAction.Style.default, handler: { saveAction -> Void in
            let textField = alert.textFields![0] as UITextField
            print("value entered by user in our textfield is: \(textField.text)")
        })
        //CANCEL BUTTON
        let cancel = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: {
            (action : UIAlertAction!) -> Void in })


        alert.addAction(save)
        alert.addAction(cancel)

        return alert
    }()


    //MARK:- UIButton Action for showing Alert Controller
    @objc func buttonClicked(btn:UIButton){
        self.present(alertEmailAddEditView, animated: true, completion: nil)
    }

    //MARK:- UITextFieldDelegate
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}
Run Code Online (Sandbox Code Playgroud)

快乐编码!:)