ios 如何在 iphone 中创建警报框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5863481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:47:37  来源:igfitidea点击:

How to create an alert box in iphone?

iphoneobjective-ciosuialertview

提问by Blane Townsend

I would like to make an alert type box so that when the user tries to delete something, it says, "are you sure" and then has a yes or no for if they are sure. What would be the best way to do this in iphone?

我想制作一个警报类型框,以便当用户尝试删除某些内容时,它会说“你确定吗”,然后如果他们确定的话,就会有一个是或否。在 iphone 中执行此操作的最佳方法是什么?

回答by Andrew

A UIAlertViewis the best way to do that. It will animate into the middle of the screen, dim the background, and force the user to address it, before returning to the normal functions of your app.

AUIAlertView是最好的方法。在返回到应用程序的正常功能之前,它会在屏幕中间设置动画,使背景变暗,并强制用户解决它。

You can create a UIAlertViewlike this:

你可以创建一个UIAlertView这样的:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"Are you sure you want to delete this.  This action cannot be undone" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];

That will display the message.

这将显示消息。

Then to check whether they tapped delete or cancel, use this:

然后要检查他们是否点击了删除或取消,请使用以下命令:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0){
        //delete it
    }
}

Make sure in your header file (.h), you include the UIAlertViewDelegateby putting <UIAlertViewDelegate>, next to whatever your class inherits from (ie. UIViewControlleror UITableViewController, etc.)

确保在您的头文件 ( .h) 中,UIAlertViewDelegate通过将<UIAlertViewDelegate>,包含在您的类继承自的任何内容旁边(即UIViewControllerUITableViewController等)

For more infomation on all the specifics of UIAlertViewscheck out Apple's Docs Here

有关在此处UIAlertViews查看Apple 文档的所有细节的更多信息

Hope that helps

希望有帮助

回答by jboi

The post is quite old, but still a good question. With iOS 8 the answer has changed. Today you'd rather use 'UIAlertController' with a 'preferredStyle' of 'UIAlertControllerStyle.ActionSheet'.

帖子很老了,但仍然是一个好问题。在 iOS 8 中,答案发生了变化。今天,您更愿意使用带有“UIAlertControllerStyle.ActionSheet”的“preferredStyle”的“UIAlertController”。

A code like this (swift) that is bound to a button:

像这样(swift)绑定到按钮的代码:

@IBAction func resetClicked(sender: AnyObject) {
    let alert = UIAlertController(
        title: "Reset GameCenter Achievements",
        message: "Highscores and the Leaderboard are not affected.\nCannot be undone",
        preferredStyle: UIAlertControllerStyle.ActionSheet)
    alert.addAction(
        UIAlertAction(
            title: "Reset Achievements",
            style: UIAlertActionStyle.Destructive,
            handler: {
                (action: UIAlertAction!) -> Void in
                gameCenter.resetAchievements()
            }
        )
    )
    alert.addAction(
        UIAlertAction(
            title: "Show GameCenter",
            style: UIAlertActionStyle.Default,
            handler: {
                (action: UIAlertAction!) -> Void in
                self.gameCenterButtonClicked()
            }
        )
    )
    alert.addAction(
        UIAlertAction(
            title: "Cancel",
            style: UIAlertActionStyle.Cancel,
            handler: nil
        )
    )
    if let popoverController = alert.popoverPresentationController {
        popoverController.sourceView = sender as UIView
        popoverController.sourceRect = sender.bounds
    }
    self.presentViewController(alert, animated: true, completion: nil)
}

would produce this output: enter image description here

会产生这个输出: 在此处输入图片说明

EDIT: The code crashed on iPad, iOS 8+. If added the necessary lines as described here: on another stack overflow answer

编辑:代码在 iPad、iOS 8+ 上崩溃。如果按照此处所述添加必要的行:on another stack overflow answer

回答by Shaba Aafreen

For swift it is very simple.

对于 swift 来说,它非常简单。

    //Creating the alert controller
    //It takes the title and the alert message and prefferred style
    let alertController = UIAlertController(title: "Hello  Coders", message: "Visit www.simplifiedios.net to learn xcode", preferredStyle: .Alert)

    //then we create a default action for the alert... 
    //It is actually a button and we have given the button text style and handler
    //currently handler is nil as we are not specifying any handler
    let defaultAction = UIAlertAction(title: "Close Alert", style: .Default, handler: nil)

    //now we are adding the default action to our alertcontroller
    alertController.addAction(defaultAction)

    //and finally presenting our alert using this method
    presentViewController(alertController, animated: true, completion: nil)

Ref: iOS Show Alert Message

参考:iOS 显示警报消息

回答by Jon Reid

Everyone's saying UIAlertView. But to confirm deletion, UIActionSheet is likely the better choice. See When to use a UIAlertView vs. UIActionSheet

每个人都在说 UIAlertView。但是要确认删除,UIActionSheet 可能是更好的选择。查看何时使用 UIAlertView 与 UIActionSheet

回答by Anand

To pop an alert message use UIAlertView.

要弹出警报消息,请使用 UIAlertView。

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"Are you sure you want to delete this." **delegate:self** cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];
[alert release];

Once you set the delegate as self you can perform your action on this method

将委托设置为 self 后,您可以在此方法上执行操作

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

回答by danner.tech

Being that UIAlertViewis now deprecated, I wanted to provide an answer to future coders that come across this.

由于UIAlertView现在已弃用,我想为将来遇到此问题的编码人员提供答案。

Instead of UIAlertView, I would use UIAlertControllerlike so:

而不是UIAlertView,我会UIAlertController像这样使用:

@IBAction func showAlert(_ sender: Any) {
  let alert = UIAlertController(title: "My Alert", message: "This is my alert", preferredStyle: UIAlertControllerStyle.alert)

  alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: {(action) in 
    alert.dismiss(animated: true, completion: nil)
  }))
  self.present(alert,animated:true, completion:nil)
}

回答by InsertWittyName

UIAlertView seems the obvious choice for confirmation.

UIAlertView 似乎是确认的明显选择。

Set the delegate to the controller and implement the UIAlertViewDelegate protocol http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html

将委托设置为控制器并实现 UIAlertViewDelegate 协议http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html

回答by Black Frog

Use the UIAlertView classto display an alert message to the user.

使用UIAlertView 类向用户显示警报消息。

回答by Richard Stelling

Use an UIAlertView:

使用UIAlertView

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Alert Title" 
                                                     message:@"are you sure?"
                                                    delegate:self 
                                           cancelButtonTitle:@"No"
                                           otherButtonTitles:@"Yes", nil];


        [av show];
        [av autorelease];

Make sure you implement:

确保您实施:

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

To handle the response.

来处理响应。

回答by Pallavi

Here i provided the alert message, which i used in my first app:

在这里,我提供了我在第一个应用程序中使用的警报消息:

@IBAction func showMessage(sender: UIButton) {
    let alertController = UIAlertController(title: "Welcome to My First App",
                              message: "Hello World",
                              preferredStyle: UIAlertControllerStyle.alert)
    alertController.addAction(UIAlertAction(title: "OK",
                                            style: UIAlertActionStyle.default,
                                            handler: nil))
    present(alertController, animated: true, completion: nil)
}

with appropriate handlers for user responses.

为用户响应提供适当的处理程序。