2011-07-29 21 views
12

Hiện nay tôi đang sử dụng đoạn mã sau để trình bày một UIAlertView:Make chức năng UIAlertView Nút kích hoạt On Press

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete" 
         message:@"Press OK to submit your data!" 
         delegate:nil 
       cancelButtonTitle:@"OK" 
       otherButtonTitles: nil]; 
    [alert show]; 
    [alert release]; 

Làm thế nào để có được nó để khi 'OK" là ép, nó gây nên một chức năng, nói -(void)submitData

Trả lời

41

LƯU Ý:

Chú ý: UIAlertView bị phản đối trong iOS 8. (. Lưu ý rằng UIAlertViewDelegate cũng bị phản đối) Để tạo và quản lý cảnh báo trong iOS 8 và sau đó, thay vì sử dụng UIAlertController với một preferredStyle của UIAlertControllerStyleAlert.

Please check this out tutorial

"deprecated" means???

Objectvie C

tập tin .h

@interface urViewController : UIViewController <UIAlertViewDelegate> { 

tập tin .m

// Create Alert and set the delegate to listen events 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete" 
               message:@"Press OK to submit your data!" 
               delegate:self 
             cancelButtonTitle:nil 
             otherButtonTitles:@"OK", nil]; 

// Set the tag to alert unique among the other alerts. 
// So that you can find out later, which alert we are handling 
alert.tag = 100; 

[alert show]; 


//[alert release]; 


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


    // Is this my Alert View? 
    if (alertView.tag == 100) { 
     //Yes 


    // You need to compare 'buttonIndex' & 0 to other value(1,2,3) if u have more buttons. 
    // Then u can check which button was pressed. 
     if (buttonIndex == 0) {// 1st Other Button 

      [self submitData]; 

     } 
     else if (buttonIndex == 1) {// 2nd Other Button 


     } 

    } 
    else { 
    //No 
     // Other Alert View 

    } 

} 

Swift

Cách Swifty là sử dụng UIAlertController mới và đóng cửa:

// Create the alert controller 
    let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert) 

    // Create the actions 
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { 
     UIAlertAction in 
     NSLog("OK Pressed") 
    } 
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { 
     UIAlertAction in 
     NSLog("Cancel Pressed") 
    } 

    // Add the actions 
    alertController.addAction(okAction) 
    alertController.addAction(cancelAction) 

    // Present the controller 
    self.presentViewController(alertController, animated: true, completion: nil) 
1

bạn cần phải thiết lập các đại biểu khi phân bổ các alertview, sau đó sử dụng một trong những phương pháp UIAlertViewDelegate để gọi phương pháp riêng của bạn, ví dụ:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete" 
               message:@"Press OK to submit your data!" 
               delegate:self 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil]; 
[alert show]; 
[alert release]; 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    [self submitData]; 
} 
1

Bạn cần phải thiết lập delegate cho UIAlertView của bạn, trước khi hiển thị nó. Sau đó làm các công việc trong callback đại biểu như vậy:

-(void)alertView:(UIAlertView*)alert didDismissWithButtonIndex:(NSInteger)buttonIndex; 
{ 
    if ([[alert buttonTitleAtIndex] isEqualToString:@"Do it"]) { 
     // Code to execute on Do it button selection. 
    } 
} 

dự án CWUIKit My qua tại https://github.com/Jayway/CWUIKit có một sự bổ sung để UIAlertView cho phép bạn làm điều tương tự nhưng với các khối. Redusing hoạt động tương tự cho cả hai tạo, hiển thị và xử lý các cảnh báo này:

[[UIAlertView alertViewWithTitle:@"My Title" 
         message:@"The Message" 
       cancelButtonTitle:@"Cancel" 
    otherTitlesAndAuxiliaryActions:@"Do it", 
           ^(CWAuxiliaryAction*a) { 
            // Code to execute on Do it button selection. 
           }, nil] show]; 
8

Nếu bạn đang sử dụng nhiều trường hợp UIAlertView không được khai báo trong giao diện của lớp bạn cũng có thể thiết lập một thẻ để xác định trường hợp trong đại biểu của bạn , ví dụ:

ở đâu đó trên đầu tệp myClass của lớp học của bạn.m

#define myAlertViewsTag 0 

tạo UIAlertView:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert" 
    message:@"please press ok or cancel" 
    delegate:self 
    cancelButtonTitle:@"Cancel" 
    otherButtonTitles:@"OK", nil]; 
alert.tag = myAlertViewsTag; 
[alert show]; 
[alert release]; 

phương pháp đại biểu:

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if (alertView.tag == myAlertViewsTag) { 
     if (buttonIndex == 0) { 
      // Do something when cancel pressed 
     } else { 
      // Do something for ok 
     } 
    } else { 
     // Do something with responses from other alertViews 
    } 
} 
0

Nếu bạn muốn sử dụng khối bạn cũng có thể sử dụng MKAdditions để đạt được điều này một cách dễ dàng ngay cả đối với nhiều UIAlertViews.

Chỉ cần sử dụng một mã số tương tự như mẫu này:

[[UIAlertView alertViewWithTitle:@"Test" 
         message:@"Hello World" 
       cancelButtonTitle:@"Dismiss" 
       otherButtonTitles:[NSArray arrayWithObjects:@"First", @"Second", nil] 
         onDismiss:^(int buttonIndex) 
{ 
    NSLog(@"%d", buttonIndex); 
} 
onCancel:^() 
{ 
    NSLog(@"Cancelled");   
} 
] show]; 

Bạn có thể tìm thêm thông tin trong hướng dẫn này: http://blog.mugunthkumar.com/coding/ios-code-block-based-uialertview-and-uiactionsheet

0

nhỏ làm rõ hơn,

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 
     //handles title you've added for cancelButtonTitle 
     if(buttonIndex == [alertView cancelButtonIndex]) { 
      //do stuff 
     }else{ 
      //handles titles you've added for otherButtonTitles 
      if(buttonIndex == 1) { 
       // do something else 
      } 
      else if(buttonIndex == 2) { 
       // do different thing 
      } 
     } 
    } 

Ví dụ,

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Need your action!" 
message:@"Choose an option to continue!" delegate:self cancelButtonTitle:@"Not Need!" 
otherButtonTitles:@"Do Something", @"Do Different", nil]; 
[alert show]; 

enter image description here

(nó iOS7 ảnh chụp màn hình)

0

Từ iOS8 của Apple cung cấp UIAlertController lớp mới mà bạn có thể sử dụng thay vì UIAlertView mà bây giờ bị phản đối, nó cũng được nêu trong thông điệp khấu hao

UIAlertView là không được chấp nhận. Sử dụng UIAlertController với một preferredStyle của UIAlertControllerStyleAlert thay

Vì vậy, bạn nên sử dụng một cái gì đó giống như

C Mục tiêu này

UIAlertController * alert = [UIAlertController 
       alertControllerWithTitle:@"Title" 
           message:@"Message" 
          preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction* yesButton = [UIAlertAction 
         actionWithTitle:@"Yes, please" 
            style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction * action) { 
            //Handle your yes please button action here 
           }]; 

    UIAlertAction* noButton = [UIAlertAction 
          actionWithTitle:@"No, thanks" 
             style:UIAlertActionStyleDefault 
            handler:^(UIAlertAction * action) { 
             //Handle no, thanks button     
            }]; 

    [alert addAction:yesButton]; 
    [alert addAction:noButton]; 

    [self presentViewController:alert animated:YES completion:nil]; 

Swift

Cách Swifty là sử dụng UIAlertController mới và đóng cửa:

// Create the alert controller 
    let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert) 

    // Create the actions 
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { 
     UIAlertAction in 
     NSLog("OK Pressed") 
    } 
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { 
     UIAlertAction in 
     NSLog("Cancel Pressed") 
    } 

    // Add the actions 
    alertController.addAction(okAction) 
    alertController.addAction(cancelAction) 

    // Present the controller 
    self.presentViewController(alertController, animated: true, completion: nil) 
Các vấn đề liên quan