2010-12-30 19 views
17

से टेक्स्ट प्राप्त करना मैं एक चेतावनी दृश्य से टेक्स्ट प्राप्त करने का प्रयास कर रहा हूं और इसे तालिका दृश्य में सूचीबद्ध करने के लिए अपने उत्परिवर्तनीय सरणी में जोड़ रहा हूं। मुझे एहसास है कि एक ऐसा सवाल है जिसे कुछ महीने पहले पोस्ट किया गया था, लेकिन मुझे नहीं पता कि दिए गए उत्तर का उपयोग कैसे करें।UIAlertView

-(IBAction)insert { 
    UIAlertView* dialog = [[UIAlertView alloc] init]; 
    [dialog setDelegate:self]; 
    [dialog setTitle:@"Enter Name"]; 
    [dialog setMessage:@" "]; 
    [dialog addButtonWithTitle:@"Cancel"]; 
    [dialog addButtonWithTitle:@"OK"]; 

    UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)]; 
    [nameField setBackgroundColor:[UIColor whiteColor]]; 
    [dialog addSubview:nameField]; 
    [dialog show]; 

    [data addObject:[nameField text]]; 
    [mainTableView reloadData]; 
हालांकि

मेरी ऐप्लिकेशन क्रैश क्योंकि यह कहता है कि मैं सूचकांक 0. क्या मैं गलत कर रहा हूँ पर एक शून्य ऑब्जेक्ट सम्मिलित करने का प्रयास कर रहा हूँ?

संपादित करें: ठीक है मुझे लगता है कि मुझे अलर्टव्यू को संभालने के लिए एक विधि याद आ रही है। तो मुझे यह मिला:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex]; 
    if([buttonTitle isEqualToString:@"Cancel"]) { 
     return; 
    } 
    else if([buttonTitle isEqualToString:@"Ok"]) { 
     [data addObject:nameField.text]; 

    } 

अब मुझे टुकड़ों को जोड़ने की ज़रूरत है, लेकिन यह सुनिश्चित नहीं है कि कैसे।

उत्तर

45

UIAlertView का उपयोग करते समय लोग एक आम गलती करते हैं कि उन्हें लगता है कि इसे show संदेश भेजना मुख्य धागा को अवरुद्ध कर देगा। ऐसा नहीं होता। स्क्रीन पर कोई अलर्ट होने के बावजूद आपका कोड निष्पादित करना जारी है। इस प्रकार, कोई मूल्य नहीं UITextField है कि आप में पारित किया है के लिए मौजूद है।

आपको क्या करने की जरूरत है अपने UIViewController में UIAlertViewDelegate लागू हड़पने के लिए जो कुछ भी एक उपयोगकर्ता पाठ क्षेत्र में प्रवेश किया है है। उस ने कहा, आपको अभी भी nil मान की जांच करनी है, क्योंकि यदि आप कुछ भी टाइप नहीं करते हैं, तो text मान nil होगा।

अपडेट: यह उत्तर UIAlertViewStyle के माध्यम से एक चेतावनी में UITextField जोड़ने के लिए एक एपीआई बनाया गया था।

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 1) { 
     NSString *name = [alertView textFieldAtIndex:0].text; 
     // Insert whatever needs to be done with "name" 
    } 
} 
+0

मेरा ऐप एक विंडो आधारित एप्लिकेशन है। क्या इससे कोई फर्क पड़ता है? – Snowman

+0

@ एफ-प्राइम नो, इससे कोई फर्क नहीं पड़ता। हम UIAlertView के पदानुक्रम को पार कर रहे हैं। –

+0

ठीक है तो मेरा पूरा कोड कैसा दिखना चाहिए? मेरी ऐड विधि प्लस आपकी क्लिक की गई बटनऑट इंडेक्स विधि और इसे काम करना चाहिए? क्या मुझे अपनी ऐड विधि में क्लिक किया गया बटनऑट इंडेक्स कॉल करना है? Cuz के रूप में अब यह काम नहीं कर रहा है, यह – Snowman

0

– alertView:clickedButtonAtIndex: UIAlertView का एक प्रतिनिधि तरीका है। बस अपने कंट्रोलर कार्यान्वयन में आपके संपादन में परिभाषित विधि जोड़ें और आपको जाने के लिए अच्छा होना चाहिए, क्योंकि आप पहले ही नियंत्रक को अलर्टव्यू के प्रतिनिधि बनने के लिए सेट करते हैं।

इसके अलावा अपने वर्ग में शीर्ष लेख में जोड़ने के लिए, किसी भी प्रतिनिधि से संबंधित चेतावनी से बचने के लिए, अर्थात्:

@interface MyViewController : UIViewController <UIAlertViewDelegate> { 
... 
}; 

मत भूलना डालने विधि से [data addObject:[nameField text]]; और [mainTableView reloadData]; दूर करने के लिए।

+0

दबाया गया था तब दर्ज किया गया टेक्स्ट का उपयोग AlertView को अपना स्वयं का पाठ क्षेत्र को जोड़ने के लिए, लेकिन इसके बजाय उचित शैली

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter Name" message:@" " delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; [alert show]; 

सेट की जरूरत नहीं है ठीक है मैंने ऐसा किया लेकिन मुझे क्लिक किए गए बटन में "नेमफील्ड अविकसित" त्रुटि मिली ButtonAtIndex विधि – Snowman

0
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Alert title" 
                message:@"alert message" 
               delegate:self 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"Ok", nil]; 
[myAlert addTextFieldWithValue:nil label:@"<place holder>"]; 
[[myAlert textField] setTextAlignment:UITextAlignmentCenter]; 
[[myAlert textField] becomeFirstResponder]; 
[myAlert show]; 
[myAlert release]; 
myAlert = nil;    

कोई ज़रूरत नहीं एक कस्टम फ़ील्ड जोड़ने के लिए: यहाँ अद्यतन कोड, UIAlertViewDelegate कॉल पीठ में फिर @matt

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter Name" 
               message:@" " 
               delegate:self 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"OK", nil]; 
alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
[alert show]; 

से उधार लिया है। आप सीधे addTextFieldWithValue विधि का उपयोग कर जोड़ सकते हैं।

+0

UIAlertView में कोई विधि नहीं है 'addTextFieldWithValue: लेबल: ' –

3

पर UITextView जोड़ने का प्रयास न करें या अन्यथा ऐप्पल आपके ऐप को अस्वीकार कर सकता है।

मैंने पहले से ही अपने ऐप में एक ही तरह की कार्यक्षमता का उपयोग किया है। लेकिन UIAlertView में UITextView के उपयोग के कारण ऐप्पल मेरी एपीपी को अस्वीकार कर दिया गया।

27

आप के बाद ठीक (बटन 1)

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 1) { 
     NSString *name = [alertView textFieldAtIndex:0].text; 
     // name contains the entered value 
    } 
} 
+0

नोट: केवल आईओएस 5 + पर काम करता है –

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