- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *alterView = [UIButton buttonWithType:(UIButtonTypeCustom)];
alterView.frame = CGRectMake(100, 100, 100, 100);
// 给alterView 添加点击方法
[alterView addTarget:self action:@selector(alterViewButton:) forControlEvents:(UIControlEventTouchUpInside)];
alterView.backgroundColor = [UIColor blueColor];
[self.view addSubview:alterView];
UIButton *actionSheet = [UIButton buttonWithType:(UIButtonTypeCustom)];
actionSheet.frame = CGRectMake(100, 260, 100, 100);
// 给actionSheet 添加点击方法
[actionSheet addTarget:self action:@selector(actionSheetButton:) forControlEvents:(UIControlEventTouchUpInside)];
actionSheet.backgroundColor = [UIColor yellowColor];
[self.view addSubview:actionSheet];
}
// alterViewButton的点击事件
- (void)alterViewButton:(UIButton *)button
{
// 初始化一个一个UIAlertController
// 参数preferredStyle:是IAlertController的样式
// UIAlertControllerStyleAlert 创建出来相当于UIAlertView
// UIAlertControllerStyleActionSheet 创建出来相当于 UIActionSheet
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"身份认证" message:@"请输入密码" preferredStyle:(UIAlertControllerStyleAlert)];
// 创建按钮
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
[self okButton:alertController.textFields.firstObject.text];
}];
// 创建按钮
// 注意取消按钮只能添加一个
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action) {
// 点击按钮后的方法直接在这里面写
[self cancelButton];
}];
// // 创建警告按钮
// UIAlertAction *structlAction = [UIAlertAction actionWithTitle:@"警告" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction *action) {
// NSLog(@"注意学习");
// }];
//
// 添加按钮 将按钮添加到UIAlertController对象上
[alertController addAction:okAction];
[alertController addAction:cancelAction];
//[alertController addAction:structlAction];
// 只有在alert情况下才可以添加文本框
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];
// 取出文本
// UITextField *text = alertController.textFields.firstObject;
// UIAlertAction *action = alertController.actions.firstObject;
// 将UIAlertController模态出来 相当于UIAlertView show 的方法
[self presentViewController:alertController animated:YES completion:nil];
}
- (void)cancelButton
{
NSLog(@"取消");
}
- (void)okButton:(NSString *)string
{
NSLog(@"确定:%@",string);
}
//actionSheet点击事件
- (void)actionSheetButton:(UIButton *)button
{
//获取手机系统版本
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version > 7.99) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {}];
UIAlertAction* fromPhotoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
// imagePicker.allowsEditing = YES;
imagePicker.allowsEditing = NO;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
}];
UIAlertAction* fromCameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
// imagePicker.allowsEditing = YES;
imagePicker.allowsEditing = NO;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}];
[alertController addAction:cancelAction];
[alertController addAction:fromCameraAction];
[alertController addAction:fromPhotoAction];
[self presentViewController:alertController animated:YES completion:nil];
} else {
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"从相册选择", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
actionSheet.delegate = self;
[actionSheet showInView:self.view];
}
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *alterView = [UIButton buttonWithType:(UIButtonTypeCustom)];
alterView.frame = CGRectMake(100, 100, 100, 100);
// 给alterView 添加点击方法
[alterView addTarget:self action:@selector(alterViewButton:) forControlEvents:(UIControlEventTouchUpInside)];
alterView.backgroundColor = [UIColor blueColor];
[self.view addSubview:alterView];
UIButton *actionSheet = [UIButton buttonWithType:(UIButtonTypeCustom)];
actionSheet.frame = CGRectMake(100, 260, 100, 100);
// 给actionSheet 添加点击方法
[actionSheet addTarget:self action:@selector(actionSheetButton:) forControlEvents:(UIControlEventTouchUpInside)];
actionSheet.backgroundColor = [UIColor yellowColor];
[self.view addSubview:actionSheet];
}
// alterViewButton的点击事件
- (void)alterViewButton:(UIButton *)button
{
// 初始化一个一个UIAlertController
// 参数preferredStyle:是IAlertController的样式
// UIAlertControllerStyleAlert 创建出来相当于UIAlertView
// UIAlertControllerStyleActionSheet 创建出来相当于 UIActionSheet
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"身份认证" message:@"请输入密码" preferredStyle:(UIAlertControllerStyleAlert)];
// 创建按钮
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
[self okButton:alertController.textFields.firstObject.text];
}];
// 创建按钮
// 注意取消按钮只能添加一个
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action) {
// 点击按钮后的方法直接在这里面写
[self cancelButton];
}];
// // 创建警告按钮
// UIAlertAction *structlAction = [UIAlertAction actionWithTitle:@"警告" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction *action) {
// NSLog(@"注意学习");
// }];
//
// 添加按钮 将按钮添加到UIAlertController对象上
[alertController addAction:okAction];
[alertController addAction:cancelAction];
//[alertController addAction:structlAction];
// 只有在alert情况下才可以添加文本框
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];
// 取出文本
// UITextField *text = alertController.textFields.firstObject;
// UIAlertAction *action = alertController.actions.firstObject;
// 将UIAlertController模态出来 相当于UIAlertView show 的方法
[self presentViewController:alertController animated:YES completion:nil];
}
- (void)cancelButton
{
NSLog(@"取消");
}
- (void)okButton:(NSString *)string
{
NSLog(@"确定:%@",string);
}
//actionSheet点击事件
- (void)actionSheetButton:(UIButton *)button
{
//获取手机系统版本
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version > 7.99) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {}];
UIAlertAction* fromPhotoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
// imagePicker.allowsEditing = YES;
imagePicker.allowsEditing = NO;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
}];
UIAlertAction* fromCameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
// imagePicker.allowsEditing = YES;
imagePicker.allowsEditing = NO;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}];
[alertController addAction:cancelAction];
[alertController addAction:fromCameraAction];
[alertController addAction:fromPhotoAction];
[self presentViewController:alertController animated:YES completion:nil];
} else {
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"从相册选择", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
actionSheet.delegate = self;
[actionSheet showInView:self.view];
}
}