2012-03-08 9 views
29

अद्यतन UIAlertView अब एक शैली जो UIAlertView यानीकैसे मैं एक UIAlertView में पाठ फ़ील्ड init कर सकते हैं

alert.alertViewStyle = UIAlertViewStylePlainTextInput; 

यह अच्छी तरह से काम करता है में एक पाठ इनपुट क्षेत्र की अनुमति देता है, लेकिन मैं एक डिफ़ॉल्ट रूप के साथ लेख इनपुट init करना चाहता था "नमूना" जैसे पाठ।

मुझे लगता है कि अतीत में लोगों की तरह गैर-दस्तावेजी एपीआई (जो अच्छा काम करता है) का इस्तेमाल किया है

[alert addTextFieldWithValue:@"sample text" label:@"Text Field"]; 

लेकिन जब से यह अभी भी एक आधिकारिक एप्पल सार्वजनिक API नहीं है मैं इसे इस्तेमाल नहीं कर सकते।

इसे संभालने का कोई और तरीका? मैंने willPresentAlertView में init करने की कोशिश की लेकिन टेक्स्ट फ़ील्ड केवल पढ़ने के लिए प्रतीत होता है।

धन्यवाद

उत्तर

8

नहीं परीक्षण किया है, लेकिन मुझे लगता है यह काम करेगा:

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:...]; 
UITextField* textField = [alert textFieldAtIndex:0]; 
textField.text = @"sample"; 
[alert show]; 
57

UIALertView एक textFieldAtIndex: विधि है कि UITextField वस्तु आप चाहते हैं देता है।

UIAlertView *alert = .... 
UITextField *textField = [alert textFieldAtIndex:0]; 
textField.placeholder = @"your text"; 

UIAlertView Class Reference

+0

महान - आपको लगता है कि – timeview

+1

काम आपका स्वागत :) कर रहे हैं यह अपने प्रश्न का उत्तर, तुम टिक अगले पर क्लिक करके जवाब स्वीकार कर सकते हैं धन्यवाद इसे – Mutix

+0

पर यह अब और काम नहीं कर रहा है। textFieldAtIndex इस त्रुटि को उत्पन्न करता है: *** अपरिपक्व अपवाद के कारण ऐप को समाप्त करना 'NSInvalidArgumentException', कारण: 'textFieldIndex (0) टेक्स्ट फ़ील्ड की सरणी की सीमाओं के बाहर है। – Symmetric

6

डिफ़ॉल्ट सेटिंग के लिए:

एक UIAlertViewStylePlainTextInput के लिए, पाठ फ़ील्ड के सूचकांक 0.

फिर आप प्लेसहोल्डर (या पाठ) सेट कर सकते हैं पाठ फ़ील्ड की संपत्ति है uiAlertView में मूल्य यह काम काम करेगा।

UIAlertView *alert = .... 
UITextField *textField = [alert textFieldAtIndex:0]; 
[textField setText:@"My default text"]; 
[alert show]; 
0
- (IBAction)showMessage:(id)sender { 
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add New Category" 
                 message:nil 
                delegate:self 
              cancelButtonTitle:@"Add" 
              otherButtonTitles:@"Cancel", nil]; 
    [message setAlertViewStyle:UIAlertViewStylePlainTextInput]; 
    [message show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 
    if([title isEqualToString:@"Add"]) 
    { 
     UITextField *username = [alertView textFieldAtIndex:0]; 
     NSLog(@"Category Name: %@", username.text); 
    } 
} 
9
करने के लिए

आसान तरीका

UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"your title" message:@"your message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
    alerView.alertViewStyle = UIAlertViewStylePlainTextInput; 
    [[alerView textFieldAtIndex:0] setPlaceholder:@"placeholder text..."]; 
    [alerView show]; 
+0

सही उत्तर प्राप्त करने का प्रयास करने से पहले :) –

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