UI控件笔记(三):UI之UIButton的属性

来源:互联网 发布:弥塞拉 知乎 编辑:程序博客网 时间:2024/04/30 11:04

 一、UIButton的相关操作


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    self.window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];

    self.window.backgroundColor = [UIColorwhiteColor];


    1UIButton的创建过程


  1.1、用类方法实例化一个UIButton对象,参数可以选择不同的button类型,现在我们选择的是圆角按钮,用来添加文字

    UIButton *firstBtn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];


    1.2、设置buttonframe

    firstBtn.frame = CGRectMake(0, 20, 320, 44);


    1.3、添加到父视图

    [self.windowaddSubview:firstBtn];

    

    注意:是否需要对firstBtn执行release?不需要release,因为是用类方法创建的

    

    1.4、设置圆角按钮的标题文字

    [firstBtn setTitle:@"pp"forState:UIControlStateNormal];

    

    2UIButton的按钮类型

    AroundRect,类型:圆角,用途:写字

    Badd,类型:加号,用途:和添加东西相关,看美工

    UIButton *secondBtn = [UIButtonbuttonWithType:5];

    secondBtn.frame = CGRectMake(0, 64, 320, 44);

    [self.windowaddSubview:secondBtn];

    

    Cinfo,类型:i号,用途:和提示相关,看美工

    UIButton *thirdBtn = [UIButtonbuttonWithType:3];

    thirdBtn.frame = CGRectMake(0, 108, 320, 44);

    [self.windowaddSubview:thirdBtn];


    注意:ios7之后,infoDarkinfoLightDetailDisclosure效果都一样了

    

    Dcustom,类型:custom,用途:贴图,让美工切

    UIButton *fourthBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    fourthBtn.frame = CGRectMake(0, 152, 121, 96);

    [self.windowaddSubview:fourthBtn];

    

    [fourthBtn setImage:[UIImageimageNamed:@"DOVE 1.png"]forState:UIControlStateNormal];

    //在使用custom的按钮时,一般要保证按钮的宽和高应该正好等于图片的宽和高

    

3、循环做按钮

    在一行中,循环做4个按钮,每个按钮的大小都是80*80,依次为文字1、贴图0+5i2-4的按钮

    NSArray *typeArr = @[@"1",@"0",@"5",@"2"];//按钮类型其实是一个枚举,都是整数

    for(int i =0;i<4;i++)

    {

        UIButton *btn = [UIButtonbuttonWithType:[typeArr[i] intValue]];//把数组中对应下标的字符串转为整数,当做这里的枚举值

        btn.frame = CGRectMake(i*80, 250, 80, 80);

        [self.windowaddSubview:btn];

        if(i == 0)

        {

            [btn setTitle:@"1"forState:UIControlStateNormal];

        }

        if(i == 1)

        {

            [btn setImage:[UIImageimageNamed:@"DOVE 1.png"]forState:UIControlStateNormal];

        }

    }

    

    UIButton *nameBtn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    nameBtn.frame = CGRectMake(0, 330, 60, 30);

    [nameBtn setTitle:@"lily"forState:UIControlStateNormal];//普通状态

    [nameBtn setTitleColor:[UIColorredColor] forState:UIControlStateNormal];//普通红字

    [nameBtn setTitle:@"lucy"forState:UIControlStateHighlighted];//高亮状态,from普通状态按下去时就是高亮

    [nameBtn setTitleColor:[UIColorgreenColor] forState:UIControlStateSelected];//选中绿字

    [nameBtn setTitle:@"pp"forState:UIControlStateSelected];//选中状态

    nameBtn.selected =NO;//按钮的选中属性,yes为选中,no为普通

    nameBtn.tag = 1000;

    [self.windowaddSubview:nameBtn];

    

4、按钮事件的添加

    IOS中触发一个button的事件方法一般是使用,目标-方法机制,就是当一个事件发生时,让哪个目标对象,执行什么方法

    [nameBtn addTarget:selfaction:@selector(nameBtnDown)forControlEvents:UIControlEventTouchUpInside];

    //这个按钮的第三个参数所代表的事件,会使得第一个参数的对象去调用第二个参数的方法


    4.1、按钮的常用事件及其使用环境

    A、按下,touchDown,长按弹出菜单

    UIControlEventTouchDown

    

    B、按下并在按钮内部松手,touchUpInside,普通点击

    UIControlEventTouchUpInside

    

    C、按下并在按钮内部拖动,touchDragInside,拖动一个按钮移动

    UIControlEventTouchDragInside

    

    注意:BC别写错了

    

    //三个按钮改label

    UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(0,20, 320, 44)];

    [self.windowaddSubview:label];

    [label release];

    //一会儿按钮点击的时候要改变这个label的文字,就得找到这个label,然后改变这个labeltext属性,就得能找到这个label,怎么找?通过tag值来找

    label.tag = 2000;

    

    for(int i =0;i<3;i++)

    {

        UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

        btn.frame = CGRectMake(i*100, 64, 100, 30);

        [btn setTitle:[NSStringstringWithFormat:@"%d",i]forState:UIControlStateNormal];

        [self.windowaddSubview:btn];

        //btn的方法

        [btn addTarget:selfaction:@selector(btnDown:)forControlEvents:UIControlEventTouchUpInside];

//这个按钮的方法带了一个UIButton类型的参数,所以哪个按钮触发了方法,参数中的btn就是哪个按钮

    }

    

    [self.windowmakeKeyAndVisible];

    return YES;

}


4.2、这是一个有参数的按钮方法,参数类型不用想,只能是UIButton*,什么的方法,就是什么的参数

-(void)btnDown:(UIButton*)btn//这个参数就是被点的按钮,就等于把被点的按钮对象传过来了

{

    NSLog(@"%@",btn.titleLabel.text);

    //改变label的文字

    

    //找到label

    UILabel *lab = (UILabel*)[self.windowviewWithTag:2000];

    //改字

    lab.text = btn.titleLabel.text;

}


4.3下面这个方法是nameBtn的点击事件,当nameBtn有一些事件的时候,会调用这个方法

-(void)nameBtnDown

{

    UIButton *tempBtn = (UIButton*)[self.windowviewWithTag:1000];//通过1000这个值找到他对应的按钮

    tempBtn.selected = !tempBtn.selected;//bool值置反,从而实现改变按钮状态的功能

    //通过改变按钮的选中或者普通状态改变按钮的文字

    NSLog(@"别乱点");

    

    //获取按钮的文字

    NSLog(@"%@",tempBtn.titleLabel.text);

}


二、UIButton小结

1、实例化方法,类方法[UIButton buttonWithType:类型]

2、类型:roundRect、custom、Add、info(三种)四种

           圆角写字   透明贴图   +号      i号

3、设置按钮的矩形:btn.frame

4、圆角按钮设置title:btn setTitle:title字符串 state:状态

5、按钮的状态:normal、highLighted、selected

     普通       按下高亮            选中

6、改变按钮状态:btn.selected,YES或NO

7、按钮的事件:首先要有一个方法给他用btn addTarget:目标对象 action:@selector(方法名) event:事件

8、按钮的事件:touchDown、touchUpInside、touchDragInside

                 按下       按下在内部松手     按下在内部拖动

9、如何找到btn上的文字:btn.titleLabel.text


0 0
原创粉丝点击