UI 02 UIButton

2 篇文章 0 订阅

UIButton 继承于 UIControl , UIControl 继承于 UIView.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor purpleColor];
    [self.window makeKeyAndVisible];
    [self.window release];


// 创建一个UIButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(150, 100, 80, 80)
    ;
    button.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:button];

    [button setTitle:@"确认" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:25];

    button.layer.cornerRadius = 8;
    button.layer.masksToBounds = YES;
    [button addTarget:self action:@selector(changePic:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = 1000;
    self.isSelected = YES;
    // 给Button设置背景图片
    [button setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];

    UIButton  *buttontwo  = [UIButton buttonWithType:UIButtonTypeCustom];
    buttontwo.frame = CGRectMake(150, 220, 80, 80);
    [self.window addSubview:button1];


  //  buttontwo.layer.cornerRadius = 8;
 //   [buttontwo setTitle:@"测试" forState:UIControlStateNormal];
 //   buttontwo.titleLabel.font = [UIFont systemFontOfSize:10];
    [buttontwo addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
    buttontwo.tag = 1001;

    // 设置前景图片 
    [buttontwo setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];
    self.isClick = NO;


    return YES;
}



- (void)click:(UIButton *)button{
//谁触发了按钮,相应的button就是那个对象.

    // 1.不管按谁,只修改的是tag是1000按钮的内容

    UIButton *but = (UIButton *)[self.window viewWithTag:1000];
    // 判断当前按钮的标题
    //currentTitle -- 获取当前按钮标题.

   if ([but.currentTitle compare:@"确认"] == 0) {
        [but setTitle:@"取消" forState:UIControlStateNormal];
    }else{
        [but setTitle:@"确认" forState:UIControlStateNormal];
 }
 // 打印出button当前的标题.
     NSLog(@"%@",but.currentTitle);


    //2.按哪个按钮,哪个按钮的内容就会改变
    if ([button.currentTitle isEqualToString:@"确认"]) {
       [button setTitle:@"取消" forState:UIControlStateNormal];
   }else{
        [button setTitle:@"确认" forState:UIControlStateNormal];
   }
    NSLog(@"%ld",button.tag);


定义一条 @property(nonatomic, assign)BOOL isSelected; 的属性  
// 更换按钮的背景图方法.
- (void)changePic:(UIButton *)button{
    //判断
    if (self.isSelected) {
        [button setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
    }else{
        [button setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
    }
    // 最后别忘了把当前的状态进行调整.
    self.isSelected = !self.isSelected;

    if ([button.currentTitle isEqualToString:@"确认"]) {
        [button setTitle:@"取消" forState:UIControlStateNormal];
    }else{
        [button setTitle:@"确认" forState:UIControlStateNormal];
    }    
}     
}



定义一条@property(nonatomic, assign)BOOL isClick;的属性.
- (void)changeImage:(UIButton *)button{
    //更换前景图片
    if (self.isClick) {
        [button setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];
    }else{
        [button setImage:[UIImage imageNamed:@"BtnOn.png"] forState:UIControlStateNormal];
    }
    self.isClick = !self.isClick;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值