使 UIAlertView 按钮触发功能 On Press

Posted

技术标签:

【中文标题】使 UIAlertView 按钮触发功能 On Press【英文标题】:Make UIAlertView Button trigger function On Press 【发布时间】:2011-07-29 15:08:13 【问题描述】:

目前我正在使用以下代码来呈现 UIAlertView:

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

我如何得到它,以便当按下“确定”时,它会触发一个函数,比如-(void)submitData

【问题讨论】:

【参考方案1】:

注意:

重要提示: UIAlertView 在 ios 8 中已弃用。(请注意,UIAlertViewDelegate 也已弃用。)要在 iOS 8 及更高版本中创建和管理警报,请改用 UIAlertController 和 UIAlertControllerStyleAlert 的首选样式。

Please check this out tutorial

"deprecated" means???

Objectvie C

.h 文件

    @interface urViewController : UIViewController <UIAlertViewDelegate> 

.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

    


斯威夫特

Swifty 的方式是使用新的 UIAlertController 和闭包:

    // 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)

【讨论】:

【参考方案2】:

如果您使用多个未在类的接口中声明的 UIAlertView 实例,您还可以在委托方法中设置标签来标识实例,例如:

类文件 myClass.m 顶部的某个位置

#define myAlertViewsTag 0

创建 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];

委托方法:

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

【讨论】:

【参考方案3】:

在分配alertview的时候需要设置delegate,然后使用其中一种UIAlertViewDelegate方法调用自己的方法,例如:

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

【讨论】:

【参考方案4】:

在显示之前,您需要为您的UIAlertView 设置delegate。然后在委托回调中做这样的工作:

-(void)alertView:(UIAlertView*)alert didDismissWithButtonIndex:(NSInteger)buttonIndex;

    if ([[alert buttonTitleAtIndex] isEqualToString:@"Do it"]) 
        // Code to execute on Do it button selection.
    

我在 https://github.com/Jayway/CWUIKit 的 CWUIKit 项目对 UIAlertView 有一个补充,它允许你做同样的事情,但有块。 Red 使用相同的操作来创建、显示和处理对此的警报:

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

【讨论】:

【参考方案5】:

如果您想使用块,您也可以使用MKAdditions 轻松实现这一点,即使对于多个 UIAlertViews。

只需使用类似于此示例的代码:

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

您可以在本教程中找到更多信息:http://blog.mugunthkumar.com/coding/ios-code-block-based-uialertview-and-uiactionsheet

【讨论】:

【参考方案6】:

再澄清一点,

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

例子,

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

(iOS7截图)

【讨论】:

【参考方案7】:

从 iOS8 Apple 提供新的 UIAlertController 类,您可以使用它来代替现在已弃用的 UIAlertView,它也在折旧消息中说明

UIAlertView 已弃用。将 UIAlertController 与首选样式一起使用 的 UIAlertControllerStyleAlert 代替

所以你应该使用这样的东西

目标 C

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

斯威夫特

Swifty 的方式是使用新的 UIAlertController 和闭包:

    // 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)

【讨论】:

以上是关于使 UIAlertView 按钮触发功能 On Press的主要内容,如果未能解决你的问题,请参考以下文章

使按钮 UIAlertView 执行 Segue

kivy:单击 1 次按钮触发多个功能

更改 UIAlertview 和 UIActionsheet 按钮的色调颜色

iPhone App 中的 UIAlertView 动态

如何使 on_touch_down 小部件具体化?

在alerview被解雇后如何使webview成为第一响应者