运行效果
示例demo
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect screen = [[UIScreen mainScreen] bounds];
UIButton *buttonAlertView = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonAlertView setTitle:@"test警告框" forState: UIControlStateNormal];
CGFloat buttonViewWidth = 100;
CGFloat buttonViewHeight = 30;
CGFloat buttonViewTopView = 130;
buttonAlertView.frame = CGRectMake((screen.size.width - buttonViewWidth) / 2, buttonViewTopView, buttonViewWidth, buttonViewHeight);
[buttonAlertView addTarget:self action:@selector(testAlertView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: buttonAlertView];
}
- (void)testAlertView:(id)sender{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Alert text goes here" preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
NSLog(@"tap no button");
}];
UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"yes" style:UIAlertActionStyleDefault handler:^ void (UIAlertAction *action){
NSLog(@"tap yes button");
}];
[alertController addAction:noAction];
[alertController addAction: yesAction];
[self presentViewController:alertController animated:true completion:^(){
NSLog(@"success");
}];
}
@end