Snail—UI学习之UIButton

UIButton的一些简单应用 ,现在写的都是一些自己用到的遇到的,一些其他的目前还没有看

#import "WJJRootViewController.h"

@interface WJJRootViewController ()

@end

@implementation WJJRootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

//当前界面的主入口 只调用一次
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    /*
     UIButtonTypeContactAdd 带一个加号标志的按钮
     UIButtonTypeCustom     自定义按钮 
     UIButtonTypeDetailDisclosure 就是按钮上一个圆圈 里面一个i 类似于某事物的细节按钮
     UIButtonTypeRoundedRect 圆角按钮
     UIButtonTypeSystem     系统按钮样式
     */
    UIButton * button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    //设置按钮的位置大小
    button1.frame = CGRectMake(20, 20, 280, 30);
    //设置按钮的背景颜色
    button1.backgroundColor = [UIColor blackColor];
    //设置按钮的标题 及当前按钮的状态为一般状态(未点击状态)
    /*
     forState:
     UIControlStateNormal:正常状态 未被点击
     UIControlStateHighlighted 高亮状态
     UIControlStateDisabled 禁用状态
     UIControlStateSelected 选中状态
     UIControlStateApplication
     UIControlStateReserved  保留状态
     */
    [button1 setTitle:@"点我1" forState:UIControlStateNormal];
    //设置其标记位
    button1.tag = 1;
    //设置按钮是否可按动 YES:可以  NO:不可用
    button1.enabled = YES;
    //为按钮添加一个事件
    /*
     第一个参数:self指本视图控制器
     第二个参数:按下按钮后要执行什么操作
     第三个参数:UIControlEventTouchUpInside 表示当按钮按下并且鼠标抬起的瞬间会触发操作
     UIControlEventTouchDown:按下去未抬起鼠标时将触发操作
     UIControlEventTouchDownRepeat 鼠标连续按下多次时将触发
     下面几个每次操作时,都会有按钮被按下去的操作
     UIControlEventTouchDragInside 按下按钮,并在此空间范围内拖动时
     UIControlEventTouchDragOutside 按钮按钮,在此空间范围外拖动
     */
    [button1 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    
    //再新建一个Button
    UIButton * button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(20, 60, 280, 30);
    button2.tag = 2;
    button2.backgroundColor = [UIColor redColor];
    [button2 setTitle:@"点我2" forState:UIControlStateNormal];
    //此action中得click:是代表有参数的click方法
    [button2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    //自定义按钮  UIButtonTypeCustom
    UIButton * button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake(20, 100, 31, 30);
    
    [button3 setTitle:@"点我3" forState:UIControlStateNormal];
    button3.tag = 3;
    //设置button的初始状态  已经选择的状态
    button3.selected = NO;
    //设置自定义按钮两种状态下得不同图片
    //未选中状态显示的是第一个图片 选中状态后是后边的一个图片
    [button3 setImage:[UIImage imageNamed:@"star_icon@2x.png"] forState:UIControlStateNormal];
    
    [button3 setImage:[UIImage imageNamed:@"star2_Gray@2x.png"] forState:UIControlStateSelected];
    [button3 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    
}

//按下按钮后,将触发的操作写在这里
- (void)click{
    NSLog(@"你点我了1");
}
//如果有多个按钮要触发同一个操作,但是又想实现不同按钮方法将执行不同操作时,就要判断一下是哪个按钮按下了
- (void)click:(UIButton *)button{
    NSLog(@"你点我了2");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

运行效果图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值