Adding a simple UIAlertView

后端 未结 10 638
囚心锁ツ
囚心锁ツ 2020-12-12 14:26

What is some starter code I could use to make a simple UIAlertView with one \"OK\" button on it?

相关标签:
10条回答
  • 2020-12-12 15:17

    When you want the alert to show, do this:

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ROFL" 
                                                        message:@"Dee dee doo doo." 
                                                        delegate:self 
                                                        cancelButtonTitle:@"OK" 
                                                        otherButtonTitles:nil];
    [alert show];
    
        // If you're not using ARC, you will need to release the alert view.
        // [alert release];
    

    If you want to do something when the button is clicked, implement this delegate method:

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
        // the user clicked OK
        if (buttonIndex == 0) {
            // do something here...
        }
    }
    

    And make sure your delegate conforms to UIAlertViewDelegate protocol:

    @interface YourViewController : UIViewController <UIAlertViewDelegate> 
    
    0 讨论(0)
  • 2020-12-12 15:26
    UIAlertView *myAlert = [[UIAlertView alloc] 
                             initWithTitle:@"Title"
                             message:@"Message"
                             delegate:self
                             cancelButtonTitle:@"Cancel"
                             otherButtonTitles:@"Ok",nil];
    [myAlert show];
    
    0 讨论(0)
  • 2020-12-12 15:30

    This page shows how to add an UIAlertController if you are using Swift.

    0 讨论(0)
  • 2020-12-12 15:30

    For Swift 3:

    let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    self.present(alert, animated: true, completion: nil)
    
    0 讨论(0)
提交回复
热议问题