2014-06-29 3 views
22

सक्षम करने के लिए UIAlertController TextField पर जांचें मेरे पास एक टेक्स्ट फ़ील्ड और दो बटन: कैंसल और सेव के साथ अलर्ट कंट्रोलर है।बटन

@IBAction func addTherapy(sender: AnyObject) 
{ 
    let addAlertView = UIAlertController(title: "New Prescription", message: "Insert a name for this prescription", preferredStyle: UIAlertControllerStyle.Alert) 

    addAlertView.addAction(UIAlertAction(title: "Cancel", 
             style: UIAlertActionStyle.Default, 
             handler: nil)) 

    addAlertView.addAction(UIAlertAction(title: "Save", 
             style: UIAlertActionStyle.Default, 
             handler: nil)) 

    addAlertView.addTextFieldWithConfigurationHandler({textField in textField.placeholder = "Title"}) 


    self.presentViewController(addAlertView, animated: true, completion: nil) 


} 

मुझे क्या करना चाहते हैं बचाने के बटन को निष्क्रिय करने के लिए पाठ फ़ील्ड पर एक चेक को लागू जब पाठ फ़ील्ड बस iOS के चित्र आवेदन की तरह खाली है जब आप एक NewAlbum बनाने चाहते है: यह कोड है। कृपया कोई मुझे बता सकता है कि क्या करना है?

उत्तर

23

मैं पहले सहेजने वाली सहेजने वाली कार्रवाई के साथ अलर्ट कंट्रोलर बनाउंगा। फिर टेक्स्टफील्ड को जोड़कर हैंडलर में इसके परिवर्तन को देखने के लिए अधिसूचना को शामिल किया गया है और उस चयनकर्ता में केवल सहेजने वाली क्रिया सक्षम संपत्ति को टॉगल करें। - मैं सेब उदाहरण से इस पैटर्न सीधे मिल गया

//hold this reference in your class 
weak var AddAlertSaveAction: UIAlertAction? 

@IBAction func addTherapy(sender : AnyObject) { 

    //set up the alertcontroller 
    let title = NSLocalizedString("New Prescription", comment: "") 
    let message = NSLocalizedString("Insert a name for this prescription.", comment: "") 
    let cancelButtonTitle = NSLocalizedString("Cancel", comment: "") 
    let otherButtonTitle = NSLocalizedString("Save", comment: "") 

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert) 

    // Add the text field with handler 
    alertController.addTextFieldWithConfigurationHandler { textField in 
     //listen for changes 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleTextFieldTextDidChangeNotification:", name: UITextFieldTextDidChangeNotification, object: textField) 
    } 


    func removeTextFieldObserver() { 
     NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextFieldTextDidChangeNotification, object: alertController.textFields[0]) 
    } 

    // Create the actions. 
    let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .Cancel) { action in 
     NSLog("Cancel Button Pressed") 
     removeTextFieldObserver() 
    } 

    let otherAction = UIAlertAction(title: otherButtonTitle, style: .Default) { action in 
     NSLog("Save Button Pressed") 
     removeTextFieldObserver() 
    } 

    // disable the 'save' button (otherAction) initially 
    otherAction.enabled = false 

    // save the other action to toggle the enabled/disabled state when the text changed. 
    AddAlertSaveAction = otherAction 

    // Add the actions. 
    alertController.addAction(cancelAction) 
    alertController.addAction(otherAction) 

    presentViewController(alertController, animated: true, completion: nil) 
} 

    //handler 
func handleTextFieldTextDidChangeNotification(notification: NSNotification) { 
    let textField = notification.object as UITextField 

    // Enforce a minimum length of >= 1 for secure text alerts. 
    AddAlertSaveAction!.enabled = textField.text.utf16count >= 1 
} 

मैं एक अन्य परियोजना में यह कर रहा हूं:

यहाँ मैं क्या कह रहा हूँ है। https://developer.apple.com/library/content/samplecode/UICatalog/Introduction/Intro.html

+0

आप मेरी बहुत मदद करते हैं! आपका बहुत बहुत धन्यवाद! – Andorath

+4

अधिसूचना का उपयोग करना बहुत भारी है (और अविश्वसनीय, और बहुत अधिक काम)। यही कारण है कि टेक्स्ट फ़ील्ड प्रतिनिधि और कार्य हैं। मेरा उत्तर यहां देखें: http://stackoverflow.com/a/25628065/341994 – matt

+1

मैट का उत्तर बेहतर है – LastMove

41

सूचना केंद्र का उपयोग किए बिना एक बहुत सरल तरीका है, तेज में:

सौरभ शर्मा से स्विफ्ट 3 प्रति जवाब: वे एक बहुत अच्छा उदाहरण परियोजना UICatalog उदाहरण में इन पैटर्न के कुछ रूपरेखा है

weak var actionToEnable : UIAlertAction? 

func showAlert() 
{ 
    let titleStr = "title" 
    let messageStr = "message" 

    let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.alert) 

    let placeholderStr = "placeholder" 

    alert.addTextField(configurationHandler: {(textField: UITextField) in 
     textField.placeholder = placeholderStr 
     textField.addTarget(self, action: #selector(self.textChanged(_:)), for: .editingChanged) 
    }) 

    let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { (_) -> Void in 

    }) 

    let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: { (_) -> Void in 
     let textfield = alert.textFields!.first! 

     //Do what you want with the textfield! 
    }) 

    alert.addAction(cancel) 
    alert.addAction(action) 

    self.actionToEnable = action 
    action.isEnabled = false 
    self.present(alert, animated: true, completion: nil) 
} 

func textChanged(_ sender:UITextField) { 
    self.actionToEnable?.isEnabled = (sender.text! == "Validation") 
} 

स्विफ्ट 2.0:

weak var actionToEnable : UIAlertAction? 

func showAlert() 
{ 
    let titleStr = "title" 
    let messageStr = "message" 

    let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert) 

    let placeholderStr = "placeholder" 

    alert.addTextFieldWithConfigurationHandler({(textField: UITextField) in 
     textField.placeholder = placeholderStr 
     textField.addTarget(self, action: #selector(self.textChanged(_:)), forControlEvents: .EditingChanged) 
    }) 

    let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (_) -> Void in 

    }) 

    let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (_) -> Void in 
     let textfield = alert.textFields!.first! 

     //Do what you want with the textfield! 
    }) 

    alert.addAction(cancel) 
    alert.addAction(action) 

    self.actionToEnable = action 
    action.enabled = false 
    self.presentViewController(alert, animated: true, completion: nil) 
} 

func textChanged(sender:UITextField) { 
    self.actionToEnable?.enabled = (sender.text! == "Validation") 
} 
+2

यह सही उत्तर – CodyMace

+1

स्विफ्ट 3 कोड पर होना चाहिए, 'actionToEnable' variable defintion –

+0

शानदार है ..! कूल उत्तर –

3

स्विफ्ट 3.0 समाधान अपडेट किया गया @spoek

द्वारा दिए गए
func showAlert() 
    { 
     let titleStr = "title" 
     let messageStr = "message" 

     let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.alert) 

     let placeholderStr = "placeholder" 

     alert.addTextField(configurationHandler: {(textField: UITextField) in 
      textField.placeholder = placeholderStr 
      textField.addTarget(self, action: #selector(self.textChanged(_:)), for: .editingChanged) 
     }) 

     let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { (_) -> Void in 

     }) 

     let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: { (_) -> Void in 
      let textfield = alert.textFields!.first! 

      //Do what you want with the textfield! 
     }) 

     alert.addAction(cancel) 
     alert.addAction(action) 

     self.actionToEnable = action 
     action.isEnabled = false 
     self.present(alert, animated: true, completion: nil) 
    } 

    func textChanged(_ sender:UITextField) { 
     self.actionToEnable?.isEnabled = (sender.text! == "Validation") 
    } 
+0

वास्तव में सहायक। धन्यवाद :) –

1

मैंने आसानी से टेक्स्ट फ़ील्ड जोड़ने और बटन को सक्षम करने और अक्षम करने के लिए यूआईएलर्ट कंट्रोलर का एक उप-वर्ग लागू किया। मूल तर्क उस सौरभ शर्मा के समान है लेकिन सब कुछ इस सबक्लास में स्वच्छता के लिए encapsulated है। यह उपयोगी होना चाहिए यदि आपकी परियोजना में ऐसी कई चेतावनी कार्यशीलताएं शामिल हैं।

public class TextEnabledAlertController: UIAlertController { 
    private var textFieldActions = [UITextField: ((UITextField)->Void)]() 

    func addTextField(configurationHandler: ((UITextField) -> Void)? = nil, textChangeAction:((UITextField)->Void)?) { 
     super.addTextField(configurationHandler: { (textField) in 
     configurationHandler?(textField) 

     if let textChangeAction = textChangeAction { 
      self.textFieldActions[textField] = textChangeAction 
      textField.addTarget(self, action: #selector(self.textFieldChanged), for: .editingChanged) 

     } 
    }) 

} 

    @objc private func textFieldChanged(sender: UITextField) { 
    if let textChangeAction = textFieldActions[sender] { 
     textChangeAction(sender) 
    } 
    } 
} 

इसके इस्तेमाल के लिये सिर्फ एक textChangeAction ब्लॉक जब पाठ फ़ील्ड जोड़ने प्रदान करते हैं:

alert.addTextField(configurationHandler: { (textField) in 
     textField.placeholder = "Your name" 
     textField.autocapitalizationType = .words 
    }) { (textField) in 
     saveAction.isEnabled = (textField.text?.characters.count ?? 0) > 0 
    } 

पूर्ण उदाहरण के लिए, the git page देखते हैं।