zoukankan      html  css  js  c++  java
  • UI组件之Button

    UIButton:按钮,可以实现用户和app的交互,父类是UIControl,事件驱动型的组件的父类都是UIControl.一般使用类方法创建一个对象,创建时指定button的类型,

     iOS7.0后采用UIButtonTypeRoundedRect没有圆角的效果

    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];

    设置正常状态UIControlStateNormal下的标题,按钮一般使用选中状态UIControlStateSelected,高亮状态UIControlStateHighLighted

    [button setTitle:@"click" forState:UIControlStateNormal];

    设置某种状态下标题的颜色(一般正常状态)

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    选中状态时的标题颜色

    [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

    设置为选中状态

    button.selected=YES;

    给按钮添加一个监听器对象self

    当用户按下并松开(UIControlEventTouchUpInside)事件发生时,由self调用action的方法响应处理

    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    UIControlEventTouchDown:当按钮按下未松开时的事件

    [button addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchDown];

    注:addTarget: 所跟参数为一个类名,给类中存放@selector()中调用的方法

    设置某种状态下标题的颜色(一般正常状态)

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    选中状态时的标题颜色

    [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

    自定义button时,继承UIButton,并选择重写以下方法

    - (CGRect)backgroundRectForBounds:(CGRect)bounds;

    - (CGRect)contentRectForBounds:(CGRect)bounds;            

    - (CGRect)titleRectForContentRect:(CGRect)contentRect;    //标题在按钮中的显示位置和大小

    - (CGRect)imageRectForContentRect:(CGRect)contentRect;    //图片在按钮中的显示位置和大小

    参数sender是发生事件的对象

    -(void)buttonClick:(UIButton *)sender

    {

        获取button当前的标题:sender.currentTitle

        获取button上的标题:sender.titleLabel.text

        获取button某种状态下的标题:[sender titleForState:UIControlStateNormal]

    }

    http://www.cnblogs.com/PaulpauL/ 版权声明:本文为博主原创文章,未经博主允许不得转载。
  • 相关阅读:
    23.什么是控制文件?
    路由器原理及路由协议
    大家帮忙看看,不会即使显示串口缓冲数据
    c# interface
    ip classless&no ip classless
    Visual Studio 2010 Visual C++ 确定要重新分发的 DLL
    fibonacci算法
    loopback端口作用
    疑惑的virtual method
    Read MBR via ReadFile function
  • 原文地址:https://www.cnblogs.com/PaulpauL/p/4796351.html
Copyright © 2011-2022 走看看