2012-03-04 11 views
16

Im tạo chế độ xem trong Xcode 4.3 và không chắc chắn cách chỉ định nhiều UIAlertView có nút riêng của chúng với các hành động riêng biệt. Hiện tại, các cảnh báo của tôi có các nút riêng, nhưng cũng giống như vậy. Dưới đây là mã của tôi.Nhiều UIAlertView; mỗi nút có các nút và hành động riêng của chúng

-(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"]]; 
    } 
} 

Cảm ơn bạn đã trợ giúp!

Trả lời

63

Có một thuộc tính hữu ích tag cho UIView (trong đó UIAlertView phân lớp từ). Bạn có thể đặt thẻ khác nhau cho mỗi chế độ xem cảnh báo.

UPDATE:

#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

tôi vẫn không thực sự hiểu thế nào tôi kết hợp đó. Khi tôi cố gắng để đặt điều đó trong tôi tiếp tục nhận được việc sử dụng các lỗi định danh không khai báo không có vấn đề gì tôi làm. Tôi sẽ đánh giá cao nếu bạn trả lời bằng mã tôi đã hiển thị và chỉnh sửa của bạn. Cảm ơn – DiscoveryOV

+0

đã cập nhật cho yêu cầu của bạn. – cxa

+0

Cảm ơn tuyệt vời, hoạt động tuyệt vời. Tôi chắc rằng bạn có thể nói rằng tôi không có kinh nghiệm tuyệt vời với điều này được nêu ra, và nó là tuyệt vời có những người như bạn để giúp chúng tôi ra ngoài. – DiscoveryOV

0

Anh ấy là đúng, nhưng bạn cần phải thêm này:

-(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 

    } 
} 

nếu buttonIndex == 1 thì bạn đang sử dụng otherbutton FIRST. 0 sẽ bị hủy. Nhưng chỉ cần làm gì cho 0

0

Hoặc bạn có thể làm điều này (kiểm tra tên tiêu đề), chỉ là một tùy chọn khác ...

-(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

dễ dàng hơn & mới hơn

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... 
    } 
} 

tất cả đã hoàn tất!

1

Nếu bạn thấy cần sử dụng các phương thức ủy nhiệm để nhận dạng cảnh báo khác nhau Sau đó, bạn cũng có thể sử dụng lớp Danh mục này để sử dụng Khối hoàn thành cho mỗi AlertView.

Alert_ActionSheetWithBlocks

Ví dụ:

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 }]; 

Tôi hy vọng đây là một trong những cách dễ nhất hơn khi so sánh với các tag phương pháp đại biểu + ..