UIAlerViewController的使用

这次使用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 = @"";
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值