2015-12-29 8 views
25

enter image description hereUIAlertController में टेक्स्टफ़िल्ल्ड कैसे जोड़ें?

enter image description here

मैं पासवर्ड बदलने के बारे में एक समारोह का एहसास करना चाहते हैं, यह इनपुट अपने पिछले पासवर्ड के लिए उपयोगकर्ता की आवश्यकता है, और मैं एक चेतावनी संवाद में यह डिजाइन, मैं बटन पर क्लिक करें "संशोधन की पुष्टि चाहते हैं "फिर पासवर्ड बदलने के लिए अन्य व्यू कंट्रोलर पर जाएं। मैंने कुछ कोड लिखा है, लेकिन मुझे नहीं पता कि अगले पल में कैसे लिखना है।

+0

https://github.com/KiritVaghela/UIAlertController –

उत्तर

22

आप नियंत्रक सचेत और अगर नया पासवर्ड रिक्त स्ट्रिंग है चेतावनी नियंत्रक के टेक्स्ट फ़ील्ड संपत्ति

साथ उन तक पहुँचने, चेतावनी फिर से पेश करने के लिए कई textfields जोड़ सकते हैं। या फिर एक और तरीका है .. पहले अक्षम पुष्टि करें बटन, यह केवल जब पाठ क्षेत्र पाठ

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"confirm the modification" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { 
    UITextField *password = alertController.textFields.firstObject; 
    if (![password.text isEqualToString:@""]) { 

     //change password 

    } 
    else{ 
     [self presentViewController:alertController animated:YES completion:nil]; 
    } 
}]; 
+0

के y है अलर्ट कंट्रोलर में टेक्स्टफील्ड कैसे प्राप्त करें। इसलिए मुझे लगता है कि यह लाइन बहुत महत्वपूर्ण है! यूआईटीएक्स्टफिल्ड * पासवर्ड = अलर्ट कंट्रोलर.टेक्स्टफिल्ड्स। फर्स्टऑब्जेक्ट; जब मुझे यह कोड पता है, तो मैं अगला कोड लिख सकता हूं। – Juice007

+0

पुष्टि करें बटन को कैसे अक्षम करें – iOSGeek

+0

कार्रवाई के ब्लॉक के भीतर 'अलर्ट कंट्रोलर' का संदर्भ देना एक बनाए रखने चक्र की ओर जाता है। आपको केवल इसे कमजोर संदर्भ के माध्यम से संदर्भित करना चाहिए: '__weak typeof (alertController) कमजोर अलर्ट = अलर्ट नियंत्रक; '...' UIAlertAction * ... '' ... पासवर्ड = weakAlert.textFields.firstObject; ' – beebcon

39

है सक्षम आप अपने textFields केवल पढ़ने के लिए संपत्ति के द्वारा चेतावनी नियंत्रक से सभी जोड़ा textfields मिलता है, आप इसे उपयोग कर सकते हैं अपने पाठ मिलता है। तरह

स्विफ्ट 3:

let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert) 
alertController.addTextField(configurationHandler: {(_ textField: UITextField) -> Void in 
    textField.placeholder = "Current password" 
    textField.isSecureTextEntry = true 
}) 
let confirmAction = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in 
    print("Current password \(String(describing: alertController.textFields?[0].text))") 
    //compare the current password and do action here 
}) 
alertController.addAction(confirmAction) 
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {(_ action: UIAlertAction) -> Void in 
    print("Canelled") 
}) 
alertController.addAction(cancelAction) 
present(alertController, animated: true, completion: { _ in }) 

नोट: alertController.textFields [0] .text वैकल्पिक है,

ऑब्जेक्टिव-सी उपयोग करने से पहले यह खोलने:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert]; 
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 
    textField.placeholder = @"Current password"; 
    textField.secureTextEntry = YES; 
}]; 
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 
    NSLog(@"Current password %@", [[alertController textFields][0] text]); 
    //compare the current password and do action here 

}]; 
[alertController addAction:confirmAction]; 
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 
    NSLog(@"Canelled"); 
}]; 
[alertController addAction:cancelAction]; 
[self presentViewController:alertController animated:YES completion:nil]; 

तकइस लाइन में, यह पहले टेक्स्टफील्ड को एलर कंट्रोलर में जोड़ा जाएगा और इसका टेक्स्ट प्राप्त होगा।

+0

मैं जानना चाहता हूं कि क्या क्या यह कोड "[अलर्ट कंट्रोलर टेक्स्टफिल्ड्स] [0] टेक्स्ट" मतलब है? – Juice007

+1

@ Juice007 इसका मतलब है कि आपको अलर्ट कंट्रोलर का पहला टेक्स्टफील्ड मिल रहा है और इसके पाठ को प्राप्त करना बहुत आसान है –

+1

आपको बनाए रखने वाले चक्र से बचने के लिए '[कमजोर चेतावनी नियंत्रक]' का उपयोग करना चाहिए। – dOM

7

यहाँ कि पाठ फ़ील्ड के वांछित तरह बनाता स्विफ्ट 4.0 के लिए एक अद्यतन जवाब है:

// Create a standard UIAlertController 
let alertController = UIAlertController(title: "Password Entry", message: "", preferredStyle: .alert) 

// Add a textField to your controller, with a placeholder value & secure entry enabled 
alertController.addTextField { textField in 
    textField.placeholder = "Enter password" 
    textField.isSecureTextEntry = true 
    textField.textAlignment = .center 
} 

// A cancel action 
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in 
    print("Canelled") 
} 

// This action handles your confirmation action 
let confirmAction = UIAlertAction(title: "OK", style: .default) { _ in 
    print("Current password value: \(alertController.textFields?.first?.text ?? "None")") 
} 

// Add the actions, the order here does not matter 
alertController.addAction(cancelAction) 
alertController.addAction(confirmAction) 

// Present to user 
present(alertController, animated: true, completion: nil) 

और यह कैसे जब पहली प्रस्तुत दिखता है: enter image description here

और पाठ स्वीकार करते समय:

enter image description here

संबंधित मुद्दे