版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sndongcheng/article/details/83241224
#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 buttonAlertViewWidth = 100;
CGFloat buttonAlertViewHeight = 30;
CGFloat buttonAlertViewTopView = 130;
CGFloat buttonAlertViewLeftView = (screen.size.width - buttonAlertViewWidth)/2;
buttonAlertView.frame = CGRectMake(buttonAlertViewLeftView, buttonAlertViewTopView, buttonAlertViewWidth, buttonAlertViewHeight);
[buttonAlertView addTarget:self action:@selector(testAlertView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonAlertView];
}
//在按钮的方法中设置alertview
- (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:^(UIAlertAction *action){
NSLog(@"Tap Yes Button");
}];
[alertController addAction:noAction];
[alertController addAction:yesAction];
//show
[self presentViewController:alertController animated:true completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end