How to create an alert box in iphone?

后端 未结 10 2127
我寻月下人不归
我寻月下人不归 2020-12-23 21:30

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 b

相关标签:
10条回答
  • 2020-12-23 21:40

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

    0 讨论(0)
  • 2020-12-23 21:43

    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

    0 讨论(0)
  • 2020-12-23 21:44

    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
    
    0 讨论(0)
  • 2020-12-23 21:51

    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

    0 讨论(0)
  • 2020-12-23 21:54

    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)
    }
    
    0 讨论(0)
  • 2020-12-23 21:57

    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.

    0 讨论(0)
提交回复
热议问题