UIAlerViewController的使用

来源:互联网 发布:mac隐藏的照片 编辑:程序博客网 时间:2024/06/06 13:02

这次使用UIAlerViewController,主要是给弹出的警告上得按钮添加方法。

布局是创建一个RootViewController,在自带的视图上添加或删除;

重点
1.UITextField如果什么都不输入的话,不是nil,而是@“”;
2.对UIAlerViewController添加方法,是通过添加UAlerAction。

 UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {            _textfield.text = @"";        }];

3.需要把当前对象的presentViewController设置为创建的ViewController。

[self presentViewController:alertController animated:YES completion:nil];
#import "RootViewController.h"@interface RootViewController ()@property(nonatomic,retain)UITextField * textfield;@property(nonatomic,retain)UIButton *button;@end@implementation RootViewController-(void)dealloc{    [_textfield release];    [super dealloc];}
- (void)viewDidLoad {    [super viewDidLoad];    self.textfield = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 30)];    _textfield.borderStyle = UITextBorderStyleRoundedRect;    _textfield.placeholder = @"请输入内容";    self.button  = [UIButton buttonWithType:UIButtonTypeCustom];    _button.frame = CGRectMake(100, 200, 50, 30);    _button.backgroundColor = [UIColor grayColor];    [_button setTitle:@"提交" forState:UIControlStateNormal];    [_button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];    [_button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:_textfield];    [self.view addSubview:_button];    // Do any additional setup after loading the view.}
-(void)click:(UIButton *)button{    //如果什么都不输入的话,不是nil,而是@“”;    if ([_textfield.text isEqualToString:@"123"]) {#pragma mark------------UIAlerView        UIAlertView *alertView  = [[UIAlertView alloc]initWithTitle:@"提示" message:@"登陆成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消",@"找回密码", nil];        [alertView show];        [alertView release];    }else{#pragma mark-------------UIAlerController        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆失败" preferredStyle:UIAlertControllerStyleAlert];        [self presentViewController:alertController animated:YES completion:nil];        UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {            _textfield.text = @"";        }];        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"重新登陆" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {            _textfield.text = @"";        }];        [alertController addAction:action1];        [alertController addAction:action2];    }}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    [self.view endEditing:YES];}-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    if (buttonIndex == 1) {        _textfield.text = @"";    }}
0 0
原创粉丝点击