zoukankan      html  css  js  c++  java
  • IOS 警告框 (UIAlertView)的使用方法

    1.普通警告框

    IOS的SDK中提供了一个方便的类库UIAlertView,配合着不同参数来使用此类可以做出大多数的警告框,如下代码是IOS最简单的警告框。

    1  UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"你好" message:@"我是普通警告框" 
    delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; 2 [alert show];

        当警告框创建完成后,我们可以在想显示的地方调用show方法。但是,如果在调试过程中跑过这一步会发现,并不是“show”这行代码一跑,我们的警告框就显示出来,而是要继续走完剩余的代码,在当前消息循环结束后,次警告框才会自动弹出。

        注:所谓的当前消息循环结束,可以理解为:流程全部跑完,比如函数一直跑,跑到函数尾部返回外面一层函数,再继续跑返回更外面一层当此类推,直到跑到最外面那层的函数的尾部,下一步就出了最外面那层函数,随后程序控制权交给当前消息循环。如果是主线程,则仍然不停循环等候新消息,如果是子线程,则此子线程结束。

       对于UIAlertView的界面来说,一般无论点击任何按钮,此警告框就会消失。

       如果想知道是按下哪一个按钮的话,需要添加警告框的代理回调方法:

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        id alertbutton = [alertView buttonTitleAtIndex:buttonIndex];
        NSLog(@"按下了[%@]按钮",alertbutton);
    }

      通过此代理方法,开发者能明确直到那个警告框的那个按钮被点击了。

      最多可以有5个按钮,更多的话界面会有异常情况发生。运行结果如下:

    2.无按钮警告框

      利用警告框的模态特性,来模拟一些屏蔽用户操作的现象。

      我们下面做一个警告框,在上面去掉所有元素,增加一个转动的进度圈来表明任务正在执行,这样就能达到我们的目的了,代码如下。

    -(void)showNoButtonAlert
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"请稍等" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        
        [alert show];
        
        alert.backgroundColor = [UIColor blackColor];
        //无敌风火轮
        UIActivityIndicatorView *actIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        //CGRect alertbounds=alert.bounds;
        
        //位置设置好
        actIndicator.frame=CGRectMake(0, 0, 30, 30);
        actIndicator.center = CGPointMake(CGRectGetWidth(alert.bounds)/2, CGRectGetHeight(alert.bounds)-40.0f);
        
        //动起来
        [actIndicator startAnimating];
        [alert addSubview:actIndicator];
        
        //过三秒消失
        [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:3.0f];
    }
    
    -(void)dismissAlert:(UIAlertView *)aAlertView
    {
        if(aAlertView)
        {
            //警告框消失
            [aAlertView dismissWithClickedButtonIndex:0 animated:YES];
        }
    }

    3.显示文本输入框的警告框(登陆框)

     在警告框上显示一个文本输入框表明用户的输入区域,文本框下面则是提供两个按钮:确认和取消。具体代码如下:

    -(void)showTextInputAlert
    {
        UITextField *txtField=nil;
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"你好" message:@"请输入新题目:
    
    
    
    " delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
        
        //显示之前配置txtField
        txtField = [alert textFieldAtIndex:0];
        if(txtField)
        {
            txtField.placeholder=@"请输入用户名";
            txtField.clearButtonMode = UITextFieldViewModeWhileEditing;
        }
        
        txtField = [alert textFieldAtIndex:1];
        if(txtField)
        {
            txtField.placeholder =@"请输入密码";
            txtField.clearButtonMode = UITextFieldViewModeWhileEditing;
        }
        //alert.tag=kalertTextInputType;
        [alert show];
        
        NSLog(@"%@",txtField.text);
    }
    
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        id clickButton=[alertView buttonTitleAtIndex:buttonIndex];
        NSLog(@"按下了%@按钮",clickButton);
        id tmp=nil;
    }

    显示效果如下:其根据alert.alertViewStyle不同,显示的也不同

  • 相关阅读:
    hdu1593(find a way to escape)
    每日学习小记 11/02
    将博客搬至CSDN
    浏览器渲染机制
    适配器模式 The Adapter Pattern
    工厂方法模式 The Factory Method Pattern
    观察者模式 The Observer Pattern
    模板方法模式 The Template Method Pattern
    命令模式 The Command Pattern
    迭代器模式 The Iterator Pattern
  • 原文地址:https://www.cnblogs.com/haibosoft/p/3674888.html
Copyright © 2011-2022 走看看