UIButton

- (void)viewDidLoad {
    
    [super viewDidLoad];
    /**
     *      设计模式 :抽象工厂 -> 工厂模式
     */
    // UIButton : UIControl: UIView :UIResponder :NSObject
    // UIControl -> UIView带点击事件 按钮有一定的容错率
    //    UIButtonTypeInfoLight 信息按钮
//    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeInfoDark];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    // 按钮的设置大多是调用方法,调用属性无效
    // 设置标题
    // 默认状态             UIControlStateNormal
    // 选中高亮状态          UIControlStateHighlighted
    // 选择状态             UIControlStateSelected
    [button1 setTitle:@"I am a button" forState:UIControlStateNormal];
    [button1 setTitle:@"I am a touched button" forState:UIControlStateHighlighted];
    
    // 设置顶部图片
    [button1 setImage:[UIImage imageNamed:@"btn_prepage"] forState:UIControlStateNormal];
    [button1 setImage:[UIImage imageNamed:@"btn_nextpage"] forState:UIControlStateHighlighted];
    
    // 设置背景图片
    [button1 setBackgroundImage:[UIImage imageNamed:@"btn_prepage"] forState:UIControlStateNormal];         // 正常状态下
    [button1 setBackgroundImage:[UIImage imageNamed:@"btn_nextpage"] forState:UIControlStateHighlighted];    // 高亮状态下
    [button1 setBackgroundImage:[UIImage imageNamed:@"btn_home"] forState:UIControlStateSelected];                // 选择状态下
    
    // 设置文字颜色
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    // 设置点击事件
    /**
     
            forControlEvents - 触发按钮的条件          - 内部点击
            action           - 触发之后执行的方法
            addTarget        - 执行方法的对象
     
     */ // 会自动将‘按钮’作为参数
    [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; // 当点击btn内部时候,触发buttonAction方法
    button1.tag = 101;
    button1.frame = CGRectMake(10, 30, 300, 50);
    button1.backgroundColor = [UIColor lightGrayColor];

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeContactAdd];
    button2.frame = CGRectMake(10, 100, 300, 30);
    button2.tag = 102;
    [button2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button1];
    [self.view addSubview:button2];
}


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


#pragma mark - button Action
- (void)buttonAction:(UIButton *)button {
    
    if (button.tag == 101) { // 通过判断按钮的tag来执行对应事件
        NSLog(@"101 is clicked");
    } else if (button.tag == 102){
        NSLog(@"102 is clicked");
        // 取到指定标示的button
       UIButton *button1 =  (UIButton *)[self.view viewWithTag:101];
//        button1.selected = YES; //default is NO
//        点击按钮是无法改变这个属性的,只有我们单独调用属性来进行设置
        button1.selected = !button1.selected;

    } else {
        NSLog(@"error");
    }
    
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值