1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad];
for (int i = 0 ; i < 2; i ++) { UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(120 + i * 120, 120, 100, 50); button.backgroundColor = [UIColor lightGrayColor]; if (i == 0) { [button setTitle:@"alertView" forState:UIControlStateNormal]; }else{ [button setTitle:@"actionSheet" forState:UIControlStateNormal]; } button.tag = 10 + i ; [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } } -(void)onClick:(UIButton*)button{ if (button.tag == 10) { NSLog(@"alertView"); [self alertView]; }else{ NSLog(@"actionSheet"); [self actionSheet]; } } -(void)actionSheet{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { NSLog(@"取消"); }]; UIAlertAction *firstBtn = [UIAlertAction actionWithTitle:@"first" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { NSLog(@"取消"); }]; [alertController addAction:cancelBtn]; [alertController addAction:firstBtn]; [self presentViewController:alertController animated:YES completion:nil]; } -(void)alertView{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { NSLog(@"取消"); }];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * textField) { textField.placeholder = @"请输入"; }]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * textField) { textField.placeholder = @"第二个文本输入框"; }]; UIAlertAction *resetBtn = [UIAlertAction actionWithTitle:@"reset" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) { UITextField *textField1 = alertController.textFields[0]; NSLog(@"%@",textField1.text); UITextField *textField2 = alertController.textFields[1]; NSLog(@"%@",textField2.text); }];
[alertController addAction:cancelBtn]; [alertController addAction:resetBtn];
[self presentViewController:alertController animated:YES completion:nil]; } @end
|