2010-08-16 4 views
22

के साथ बटन क्लिक करना मैं बटन दबाए जाने पर कॉल करने और चेतावनी देने का प्रयास कर रहा हूं। मैं इसका उपयोग करता हूं:UIAlertView

-(IBAction)Add { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"add button pressed" 
                message:@"Add to record" 
                delegate:nil  
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK", nil ]; 
    [alert show]; 
    [alert release];  
} 

ठीक है, कोई समस्या नहीं है, दो बटन आया, ठीक है और रद्द करें। अब मैं यह जानना चाहता हूं कि कौन सा बटन दबाया गया है मैं उपयोग करता हूं:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    // the user clicked one of the OK/Cancel buttons 
    if (buttonIndex == 0) 
    { 
     //just to show its working, i call another alert view 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well" 
                 message:@"no error"                           delegate:nil 
               cancelButtonTitle:@"IWORKS" 
               otherButtonTitles:@"NO PRB", nil]; 
     [alert show]; 
     [alert release];  


    } 
    else 
    { 
     NSLog(@"cancel");  
    } 
} 

अब समस्या है। मैं नहीं जानता कि कौन सा बटन दबाया गया है; दूसरा अलर्टव्यू नहीं दिखाता है। मैंने कोड को दो बार जांच लिया है, इसमें कोई समस्या नहीं प्रतीत होती है। कोई त्रुटि/चेतावनी भी नहीं।

+2

एक और चेतावनी के बाद सीधे एक चेतावनी दिखा रहा है शायद एक अच्छा विचार नहीं है - बस एक टिप। – vakio

उत्तर

32

बटन क्लिक का पता लगाने के लिए अलर्ट व्यू में प्रतिनिधि प्रतिनिधि होना चाहिए, उदा।

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"add button pressed" 
               message:@"Add to record" 
               delegate:self // <------ 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"OK", nil]; 
+2

+1: मैं लेखन के माध्यम से आधा रास्ते क्या था। मैं यह भी उल्लेख करता हूं कि व्यू कंट्रोलर को 'UIAlertViewDelegate' को लागू करने की आवश्यकता है (हालांकि मुझे विश्वास है कि यह चेतावनी देता है अगर यह नहीं करता है।) – sdolan

7

बटन का इंडेक्स रद्द बटन है। मैं का उपयोग कर की सिफारिश करेंगे:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
if (buttonIndex == 0) 
{ 
     NSLog(@"cancel"); 
} 
else 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OK works" message:@"no error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];                            
    [alert show]; 
    [alert release];   
} 
} 
+2

या बेहतर,' अगर (बटन इंडेक्स == अलर्ट व्यू कंसलबटन इंडेक्स) ... ' – mojuba

1

आप अपने कोड चाहें, तो क्लीनर होने के लिए और प्रतिनिधि पर कोई निर्भरता, आप UIAlertView के ब्लॉक कार्यान्वयन प्रयास करना चाहिए:

https://github.com/steipete/PSAlertView

ब्लाकों केवल iOS पर समर्थन कर रहे हालांकि 4+ डिवाइस।

13

यह आपका कोड है जिसका मैंने उपयोग किया और कुछ भी मेरे कोड को जोड़ा। **

-(IBAction) Add 
{ 

      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"add button pressed" 

                 message:@"Add to record" 
                 delegate:nil  
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK", nil]; 

     alert.tag=101;//add tag to alert 
     [alert show]; 
     [alert release];  
} 

अब जब आप अलर्ट पर बटन दबाएँ यह clickedButtonAtIndex फोन करेगा लेकिन वहाँ हर अलर्ट के लिए एक पहचानकर्ता चाहिए। तो टैग जोड़ें और फिर

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex** 

{ 

// the user clicked one of the OK/Cancel buttons 
if(alert.tag=101)  // check alert by tag 
{  

if (buttonIndex == 0) 

    { 

    //just to show its working, i call another alert view 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well" 

                message:@"no error" 
                delegate:nil 
              cancelButtonTitle:@"IWORKS" 
              otherButtonTitles:@"NO PRB", nil]; 

    [alert show]; 
    [alert release];  

} 

else 

{ 
    NSLog(@"cancel");  
} 
} 
} 

आशा है कि इससे मदद मिलती है।

2

मैं अगर तुम

- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 

} 

प्रतिनिधि विधि का उपयोग करने में किसी मौजूदा अलर्ट देखने का बटन क्लिक करें घटना पर कोई नया अलर्ट दृश्य दिखाने के लिए, यह बेहतर होगा इच्छा महसूस की बजाय

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 

} 
2
1) 
.h file 
@interface MyClassViewController:<UIAlertViewDelegate> 

2) 
.m file 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note" 
               message:@"some message" 
               delegate:self // must be self to call clickedButtonAtIndex 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"OK", nil]; 


3) 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == [alertView cancelButtonIndex]) { 
    NSLog(@"The cancel button was clicked from alertView"); 
    } 
else { 

} 
} 

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

  • कोई संबंधित समस्या नहीं^_^