2016-04-21 11 views
5

से प्रदर्शित होने से रोकें मेरे पास स्विफ्ट में UIAlertController (अलर्ट शैली) है और यह सब ठीक काम करता है। हालांकि, UITextField जिसे मैंने इसमें जोड़ा है वह एक वैकल्पिक फ़ील्ड है जिसे उपयोगकर्ता को टेक्स्ट दर्ज करने की आवश्यकता नहीं है। समस्या यह है कि जब मैं यह UIAlertController दिखाता हूं, तो कीबोर्ड डिफ़ॉल्ट रूप से चयनित टेक्स्ट फ़ील्ड के साथ एक साथ दिखाई देता है। मैं नहीं चाहता कि कुंजीपटल तब तक दिखाई दे जब तक कि उपयोगकर्ता UITextField टैप न करे। यह कैसे किया जा सकता है?कीबोर्ड को स्वचालित रूप से UIAlertController

let popup = UIAlertController(title: "My title", 
     message: "My message", 
     preferredStyle: .Alert) 
    popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in 
     optionalTextField.placeholder = "This is optional" 
    } 
    let submitAction = UIAlertAction(title: "Submit", style: .Cancel) { (action) -> Void in 
     let optionalTextField = popup.textFields![0] 
     let text = optionalTextField.text 
     print(text) 
    } 
    let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil) 
    popup.addAction(cancelAction) 
    popup.addAction(submitAction) 
    self.presentViewController(popup, animated: true, completion: nil) 
+2

आप becomeFirstResponder कहीं और फोन बनाने किया जाना चाहिए। या आप स्वयं को कॉल कर सकते हैं। अंत में संपादन: चेतावनी प्रस्तुत करने के बाद/बाद में हाँ। –

उत्तर

4

इस चाल करना चाहिए:

अपने ViewController अनुरूप UITextFieldDelegate

आवंटित कर popup.textFields![0].delegateself

popup.textFields![0] के लिए अद्वितीय टैग को जोड़ने के लिए (मैं 999 उदाहरण में नीचे प्रयोग किया जाता)

इस

को लागू करें
func textFieldShouldBeginEditing(textField: UITextField) -> Bool { 
    if textField.tag == 999 { 
    textField.tag = 0 
    return false 
    }else{ 
    return true 
    } 
} 

अपने कोड इस तरह दिखना चाहिए:

let popup = UIAlertController(title: "My title", 
            message: "My message", 
            preferredStyle: .Alert) 
    popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in 
     optionalTextField.placeholder = "This is optional" 
    } 
    popup.textFields![0].delegate = self 
    popup.textFields![0].tag = 999 
    let submitAction = UIAlertAction(title: "Submit", style: .Cancel) { (action) -> Void in 
     let optionalTextField = popup.textFields![0] 
     let text = optionalTextField.text 
     print(text) 
    } 
    let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil) 
    popup.addAction(cancelAction) 
    popup.addAction(submitAction) 
    self.presentViewController(popup, animated: true, completion: nil) 
+1

हाहा महान दिमाग एक जैसे सोचते हैं एह: पी –

3

मुझे लगता है कि यह एक चेतावनी में किसी पाठ फ़ील्ड के लिए डिफ़ॉल्ट व्यवहार है, हो सकता है एक विकल्प के डिजाइन पर विचार तो यह है कि पाठ फ़ील्ड केवल पता चलता है जब यह आवश्यक है ...

अब जिस तरह से चलो इसे बाधित करते हैं!

जब आप टेक्स्ट फ़ील्ड जोड़ते हैं तो अपना व्यू कंट्रोलर इसे प्रतिनिधि बनाएं और इसमें एक टैग जोड़ें।

जैसे

popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in 
    optionalTextField.placeholder = "This is optional" 
    optionalTextField.delegate = self 
    optionalTextField.tag = -1 
} 

तो textFieldShouldBeginEditing लागू()

func textFieldShouldBeginEditing(textField: UITextField!) { 
    if textField.tag == -1 { 
     textField.tag = 0 
     return false 
    } else { 
     return true 
    } 
} 
संबंधित मुद्दे