How to dismiss an UIAlertController and the keyboard simultaneously?

后端 未结 8 1237
离开以前
离开以前 2021-02-05 13:04

I have created a signup form with a UIAlertController and used the method addTextFieldWithConfigurationHandler to add a text field. But there is a litt

相关标签:
8条回答
  • 2021-02-05 13:32

    I had the exact same problem you had and found the solution incidentally. You probably don't need this anymore, but for the sake of others like me, here is the answer:

    Swift:

    override func canBecomeFirstResponder() -> Bool {
        return true
    }
    

    Objective-C:

    - (BOOL)canBecomeFirstResponder {
        return true;
    }
    

    Just add this code in the view controller handling the alert. Only tested in swift.

    0 讨论(0)
  • 2021-02-05 13:41

    Swizzle viewWillDisappear method for UIAlertController, and perform resignFirstResponder on correspodent text field or call endEditing: on controller's view

    I am using for this ReactiveCocoa:

            let alert = UIAlertController(title: "", message: "", preferredStyle: .Alert)
    
            alert.addTextFieldWithConfigurationHandler {
               textField in
            }
    
            let textField = alert.textFields!.first!
    
            alert.rac_signalForSelector(#selector(viewWillDisappear(_:)))
                 .subscribeNext {
                     _ in
                     textField.resignFirstResponder()
                 }
    
    0 讨论(0)
提交回复
热议问题