//
// ViewController.h
//
//
// Created by pengjiaxin on 2017/10/16.
// Copyright © 2017年 pengjiaxin. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
//定义一个警告对话框视图对象
UIAlertView *_alertView;
}
@property (nonatomic, strong) UIAlertView *alertView;
@end
//
// ViewController.m
//
//
// Created by pengjiaxin on 2017/10/16.
// Copyright © 2017年 pengjiaxin. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UIAlertViewDelegate
>
@end
@implementation ViewController
//同步---
@synthesize alertView = _alertView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 40);
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitle:@"警示对话框" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)buttonAction:(UIButton *)button{
_alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"alertMessage" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"OK",@"11", nil];
[_alertView show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//0:cancelButtonTitle:取消
//1:ok
//2:
NSLog(@"%ld",(long)buttonIndex);
}
//对话框即将消失
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(@"即将消失");
}
//对话框已经消失
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(@"已经消失");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end