2012-03-04 11 views
16

मैं एक्सकोड 4.3 में एक दृश्य बना रहा हूं और मुझे यह सुनिश्चित नहीं है कि एकाधिक UIAlertView के निर्दिष्ट कैसे करें जिनके अलग-अलग कार्यों के साथ अपने स्वयं के बटन हैं। वर्तमान में, मेरे अलर्ट के अपने बटन हैं, लेकिन एक ही क्रियाएं हैं। नीचे मेरा कोड है।एकाधिक UIAlertView; प्रत्येक अपने बटन और क्रियाओं के साथ

-(IBAction)altdev { 
    UIAlertView *alert = [[UIAlertView alloc] 

          initWithTitle:@"titleGoesHere" 
          message:@"messageGoesHere" 
          delegate:self 
          cancelButtonTitle:@"Cancel" 
          otherButtonTitles:@"Continue", nil]; 
[alert show]; 
} 

-(IBAction)donate { 
    UIAlertView *alert = [[UIAlertView alloc] 

          initWithTitle:@"titleGoesHere" 
          message:@"messageGoesHere" 
          delegate:self 
          cancelButtonTitle:@"Cancel" 
          otherButtonTitles:@"Continue", nil]; 
    [alert show]; 
} 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 1) { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite1.com"]]; 
     } 
} 


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 1) { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"examplesite2.com"]]; 
    } 
} 

किसी भी मदद के लिए धन्यवाद!

उत्तर

63

UIView (जो UIAlertView से उप-वर्ग) के लिए उपयोगी संपत्ति tag है। आप प्रत्येक अलर्ट व्यू के लिए अलग-अलग टैग सेट कर सकते हैं।

अद्यतन:

#define TAG_DEV 1 
#define TAG_DONATE 2 

- (IBAction)altdev { 
    UIAlertView *alert = [[UIAlertView alloc] 

          initWithTitle:@"titleGoesHere" 
          message:@"messageGoesHere" 
          delegate:self 
          cancelButtonTitle:@"Cancel" 
          otherButtonTitles:@"Continue", nil]; 
    alert.tag = TAG_DEV; 
    [alert show]; 
} 

- (IBAction)donate { 
    UIAlertView *alert = [[UIAlertView alloc] 

          initWithTitle:@"titleGoesHere" 
          message:@"messageGoesHere" 
          delegate:self 
          cancelButtonTitle:@"Cancel" 
          otherButtonTitles:@"Continue", nil]; 
    alert.tag = TAG_DONATE; 
    [alert show]; 
} 

-(void)alertView:(UIAlertView *)alertView 
clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (alertView.tag == TAG_DEV) { // handle the altdev 
     ... 
    } else if (alertView.tag == TAG_DONATE){ // handle the donate 

    } 
} 
+0

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

+0

आपके अनुरोध के लिए अपडेट किया गया। – cxa

+0

बहुत बढ़िया धन्यवाद, महान काम करता है। मुझे यकीन है कि आप बता सकते हैं कि मैं अभी तक इसके साथ आश्चर्यजनक अनुभव नहीं कर रहा हूं, और यह बहुत अच्छा है कि आपके जैसे लोग आपकी मदद करने के लिए हैं। – DiscoveryOV

0

वह सही है, लेकिन आप इस जोड़ने की जरूरत:

-(void)alertView:(UIAlertView *)alertView 
clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (alertView.tag == TAG_DEV && buttonIndex == 1) { // handle the altdev 
     ... 
    } else if (alertView.tag == TAG_DONATE && buttonIndex == 1){ // handle the donate 

    } 
} 

अगर buttonIndex == 1 तो आप सबसे पहले otherbutton उपयोग कर रहे हैं। 0 रद्द करने के लिए होगा। लेकिन 0

0

के लिए बस कुछ भी नहीं करें या आप ऐसा कर सकते हैं (शीर्षक का नाम देखें), बस एक और विकल्प है ... हालांकि समान रूप से शीर्षक अलर्ट लिखें!

-(IBAction)altdev { 
UIAlertView *alert = [[UIAlertView alloc] 

         initWithTitle:@"titleOneGoesHere" 
         message:@"messageGoesHere" 
         delegate:self 
         cancelButtonTitle:@"Cancel" 
         otherButtonTitles:@"Continue", nil]; 
[alert show]; 
} 

-(IBAction)donate { 
UIAlertView *alert = [[UIAlertView alloc] 

         initWithTitle:@"titleTwoGoesHere" 
         message:@"messageGoesHere" 
         delegate:self 
         cancelButtonTitle:@"Cancel" 
         otherButtonTitles:@"Continue", nil]; 
[alert show]; 
} 

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

if (buttonIndex == 1) 
{ 
    if([[alertView title] isEqualToString:@"titleOneGoesHere"]) 
    { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite1.com"]]; 
    } 
    else 
    { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"examplesite2.com"]]; 
    } 
} 
8

आसान & नए

UIAlertView *alert = [[UIAlertView alloc] init... 
alert.tag = 1; 

UIAlertView *alert = [[UIAlertView alloc] init... 
alert.tag = 2; 



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if(alertView.tag == 1) { 
     // first alert... 
    } else { 
     // sec alert... 
    } 
} 

सब किया है!

1

यदि आपको अलर्ट व्यू को अलग-अलग पहचानने के लिए प्रतिनिधि विधियों का उपयोग करने में भिन्नता मिलती है तो आप प्रत्येक अलर्ट व्यू के लिए समापन ब्लॉक का उपयोग करने के लिए इस श्रेणी वर्ग का भी उपयोग कर सकते हैं।

Alert_ActionSheetWithBlocks

उदाहरण के लिए

UIAlertView* alert1 = [[UIAlertView alloc] initWithTitle:@"AlertView+Block 1" message:@"WithBlocks" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 

[alert1 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView1 Stuff here }]; 

UIAlertView* alert2 = [[UIAlertView alloc] initWithTitle:@"AlertView+Block 2" message:@"WithBlocks" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
[alert2 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView2 Stuff here }]; 

मुझे आशा है कि यह एक और सबसे आसान तरीका है के रूप में + प्रतिनिधि विधि टैग करने के लिए तुलना है ..

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