zoukankan      html  css  js  c++  java
  • UIAlertView与UIActionSheet

    1.UIAlertView(警告框)

    1.1 创建警告框,设置样式

    - (IBAction)alertView:(UIButton *)sender {//创建button按钮

        //创建警告框的实例

        //UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"提示信息message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

        //如有多个otherButtonTitles,先显示otherButtonTitlesotherButtonTitles有一个,cancelButtonTitle在左边

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"提示信息message" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES",nil];

        //显示警告框

        [alert show];//系统自带show方法

    }

    效果如下图:

       

    1.2 创建有提示信息输入的警告框

    - (IBAction)alertViewInput:(id)sender {

     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"标题" message:@"输入您的用户信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

        //设置警告框的样式

        alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

        //显示警告框

        [alert show];

    }

    效果如下图:

     1.3创建有提示信息输入的警告框

        UIAlertViewStyleDefault

        UIAlertViewStyleSecureTextInput,//单行密码形式

        UIAlertViewStylePlainTextInput, //单行正常形式

        UIAlertViewStyleLoginAndPasswordInput//用户名和密码

        alert.alertViewStyle = UIAlertViewStylePlainTextInput;

    1.4 如何获取用户在警告框上的选择——委托

      a)什么是委托:

    一个对象(对象A)让另一个对象(对象B)帮它做事情(发消息)。

      b)委托协议:

    是委托方要求代理方所符合的规则,这些规则对应的就是一组方法,并且每个方法的第一个参数都是委托方的引用,每个方法的名称用于描述方法的执行时机

      c)设置控制器实例成为alertView的代理方的步骤

    1)控制器遵守协议//委托方定义协议的名称要求"委托方name+Delegate"

    2)控制器实现协议中的方法//要想成为代理方,必须满足委托方定义的协议

             //第一个参数永远是委托方的引用,点进协议直接复制

    3)在创建UIAlertView时设定控制器实例为代理方法

    例:通过输入用户名和密码信息,点击按钮。取出用户名和密码

    /*1.设置控制器成为警告框的代理,需要控制器(代理方)遵守协议*/

    @interface AlertViewController ()<UIAlertViewDelegate>

    @end

    @implementation AlertViewController

    - (IBAction)alertViewDelegate:(id)sender {

    //3.设置了警告框的代理方为当前控制器实例,于是,当用户点击了警告框上的某个按钮时该动作的处理就交给了控制器实例来响应

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"your choice" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES",nil];  //self

        alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

        [alert show];

    }

    /*2.设置控制器成为警告框的代理,实现协议中的方法选择哪个方法实现,根据不同的方法对应的执行时机可以从方法名判断发消息的时机。

     方法的第一个参数一定是委托方的引用

     */

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

    {

        //目标:当点击YES按钮时,在控制器打印输入的用户名和密码

        if (alertView.cancelButtonIndex !=buttonIndex) {

            //获取用户名

            NSString *loginName =[alertView textFieldAtIndex:0].text;

            //获取密码

            NSString *pwd =[alertView textFieldAtIndex:1].text;

            NSLog(@"用户名:%@ ,密码:%@",loginName,pwd);

        }

    }

    结果:

    用户名:yang ,密码:123

     

    1.5 如何区分用户点击的按钮

    在  -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex方法中,第二个参数为点击的按钮的索引,可以使用以下几种方法进行判断

    方法一:直接判断索引值区分不同的按钮

    方法二:根据索引值获取按钮的title,进行区分

    方法三:判断索引是否是cancelButtonIndex进行区分

    //根据点击的按钮的索引获取点击的按钮的标题

        NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

        方法1:判断标题来区分不同的动作

        if ([title isEqualToString:@"YES"]) {

        //    NSLog(@"点击了YES按钮");//根据点击了按钮OK执行的动作    }else{

        //    NSLog(@"点击了NO按钮");

        }

        方法3:也可以通过判断是不是cancel按钮的索引

        //来判断是否点击了cancel按钮

        if (alertView.cancelButtonIndex == buttonIndex)

        {

        //    NSLog(@"点击了NO按钮");

        }

    }

    1.6 如何获取alertView中输入框内的文本

    利用alertView的textFieldAtIndex:方法,获得不同索引位置上的文本框,然后反问text属性即可

           NSString *pwd = [alertView textFieldAtIndex:1].text;

    2.UIActionSheet(操作表)

    2.1 创建操作表

    //创建actionSheet的实例

    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"QQ空间" otherButtonTitles:@"微博",@"微信",@"朋友圈", nil];

    //显示actionSheet

    [sheet showInView:self.view];

    2.2 判断用户点击的不同的按钮

    a)需要控制器实例遵守协议

    b)需要控制器实现协议中的方法

    #import "ActionSheetViewController.h"

    @interface ActionSheetViewController ()<UIActionSheetDelegate>//遵守协议

    @end

     - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

    {

        //区分点击的按钮

        //NSLog(@"%d",buttonIndex);//获取按钮标题对应的值

        //获取按钮上的标题

        //NSLog(@"%@",[actionSheet buttonTitleAtIndex:buttonIndex]);

        if (actionSheet.cancelButtonIndex == buttonIndex) {

            //....点击了cancel

        }

        if (actionSheet.destructiveButtonIndex == buttonIndex) {

            //....点击了有破坏性的那个危险按钮

        }

    }

    效果如下图:

  • 相关阅读:
    Linux如何自动获取IP地址
    jq操作select集合
    UDP and TCP
    IPv6
    DHCP, NAT
    队列之顺序存储实现
    ARP, Fragmentation and Reassembly
    Classless Interdomain Routing (CIDR)
    Subnet Routing Examples
    Subnetting
  • 原文地址:https://www.cnblogs.com/yhj1787354782/p/5051515.html
Copyright © 2011-2022 走看看