使 UIAlertView Button 触发功能 On Press

发布于 2024-11-26 18:26:44 字数 431 浏览 0 评论 0原文

目前,我正在使用以下代码来呈现 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

Currently I am using the following code to present a 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];

How do I get it so that when 'OK" is pressed, it triggers a function, say -(void)submitData

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

神爱温柔 2024-12-03 18:26:44

注意:

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

请查看此教程

“已弃用”是什么意思???

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)

NOTE:

Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert.

Please check this out tutorial

"deprecated" means???

Objectvie C

.h file

    @interface urViewController : UIViewController <UIAlertViewDelegate> {

.m file

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

The Swifty way is to use the new UIAlertController and closures:

    // 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)
往日情怀 2024-12-03 18:26:44

如果您使用未在类接口中声明的多个 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
    }
}

If you are using multiple UIAlertView instances that are not declared in the class's interface you can also set a tag to identify instances in your delegate method, for example:

somewhere on top of your class file myClass.m

#define myAlertViewsTag 0

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

the delegate method:

-(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
    }
}
小兔几 2024-12-03 18:26:44

您需要在分配alertview时设置委托,然后使用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];
}

You need to set the delegate when allocating the alertview, then use one of the UIAlertViewDelegate methods to call your own method, for example:

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];
}
葬花如无物 2024-12-03 18:26:44

在显示之前,您需要为 UIAlertView 设置 delegate。然后在委托回调中进行如下工作:

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

我的 CWUIKit 项目位于 https://github.com/Jayway/CWUIKit UIAlertView 进行了补充,允许您使用块执行相同的操作。重复使用相同的操作来创建、显示和处理警报:

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

You need to setup the delegate for your UIAlertView, before showing it. Then do the work in the delegate callback as such:

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

My CWUIKit project over at https://github.com/Jayway/CWUIKit has an addition to UIAlertView that allow you to do the same thing but with blocks. Redusing the same operation for both creating, showing and handling the alert to this:

[[UIAlertView alertViewWithTitle:@"My Title"
                         message:@"The Message"
               cancelButtonTitle:@"Cancel"
  otherTitlesAndAuxiliaryActions:@"Do it", 
                                 ^(CWAuxiliaryAction*a) {
                                    // Code to execute on Do it button selection.
                                 }, nil] show];
青春有你 2024-12-03 18:26:44

如果你想使用块也可以使用 MKAdditions 来实现即使对于多个 UIAlertView,这也很容易。

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

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

If you want to use blocks you can also use MKAdditions to achieve this easily even for multiple UIAlertViews.

Just use a code similar to this sample:

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

You can find more information in this tutorial: http://blog.mugunthkumar.com/coding/ios-code-block-based-uialertview-and-uiactionsheet

御守 2024-12-03 18:26:44

更多说明,

- (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 屏幕截图)

Little more clarification,

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

Example,

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

(it's iOS7 screenshot)

千と千尋 2024-12-03 18:26:44

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

From iOS8 Apple provide new UIAlertController class which you can use instead of UIAlertView which is now deprecated, its is also stated in depreciation message

UIAlertView is deprecated. Use UIAlertController with a preferredStyle
of UIAlertControllerStyleAlert instead

So you should use something like this

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

Swift

The Swifty way is to use the new UIAlertController and closures:

    // 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文