Displaying Alerts with UIAlertView

UIAlertView *alertView = [[UIAlertView alloc]
                            initWithTitle:@"Alert"
                            message:@"You've been delivered an alert"
                            delegate:nil
                            cancelButtonTitle:@"Cancel"
                            otherButtonTitles:@"Ok", nil];
[alertView show];

 self.view.backgroundColor = [UIColor whiteColor];

NSString *message = @"Are you sure you want to open this link in Safari?";
UIAlertView *alertView = [[UIAlertView alloc]
                            initWithTitle:@"Open Link"
                            message:message
                            delegate:self
                            cancelButtonTitle:[self noButtonTitle]
                            otherButtonTitles:[self yesButtonTitle], nil];
[alertView show];

 more samples

// one button alert
UIAlertView *alert =
    [[UIAlertView alloc] 
    initWithTitle: @"Hello"
    message: @"Hello Master HaKu!"
    delegate: self
    cancelButtonTitle: @"OK"
    OtherButtonTitles: nil];
[alert show];
[alert release];

                            
// two buttons alert
UIAlertView *alert =
    [[UIAlertView alloc] 
    initWithTitle: @"Hello"
    message: @"Hello Master HaKu!"
    delegate: self
    cancelButtonTitle: @"YES"
    OtherButtonTitles: @"NO", nil];
[alert show];
[alert release];

                            
// more buttons alert
UIAlertView *alert =
    [[UIAlertView alloc] 
    initWithTitle: @"Hello"
    message: @"Hello Master HaKu!"
    delegate: self
    cancelButtonTitle: @"OK"
    OtherButtonTitles: @"Option1", @"Option2", nil];
[alert show];
[alert release];                            



@interface AlertTestViewController : UIViewController<UIAlertViewDelegate>

@end

// alert click implmentation
- (void) alertView: (UIAlertView *)alertView
clickedButtonAtIndex: (NSInteger)buttonIndex
{
    NSLog(@"%d", buttonIndex);
}
原文地址:https://www.cnblogs.com/davidgu/p/3461369.html