How to create an alert box in iphone?

落花浮王杯 提交于 2019-11-28 18:56:24

问题


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?


回答1:


A UIAlertView is 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.

You can create a UIAlertView like this:

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 UIAlertViewDelegate by putting <UIAlertViewDelegate>, next to whatever your class inherits from (ie. UIViewController or UITableViewController, etc.)

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

Hope that helps




回答2:


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

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

@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:

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




回答3:


For swift it is very simple.

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




回答4:


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




回答5:


UIAlertView seems the obvious choice for confirmation.

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




回答6:


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




回答7:


Use an 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.




回答8:


To pop an alert message use 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

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



回答9:


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

Instead of UIAlertView, I would use UIAlertController like so:

@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)
}



回答10:


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.



来源:https://stackoverflow.com/questions/5863481/how-to-create-an-alert-box-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!