UI_Button(按钮)


UI_Button


AppDelegate.m


#import "AppDelegate.h"


@interface AppDelegate ()


//记录当前button的点击状态

@property (nonatomic, assign)BOOL isSelected;


@end


@implementation AppDelegate


- (void)dealloc

{

    [_window release];

    [super dealloc];

}



- (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 whiteColor];

    [self.window makeKeyAndVisible];

    

    //使用button的遍历构造器的方法进行创建, 便利构造器,不用释放内存

    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];

//    UIButton *myButton2 = [[UIButton alloc]initWithFrame:CGRectMake(100, 400, 60, 30)];

    

    //位置

    myButton.frame = CGRectMake(100, 300, 30, 30);

    

    myButton.layer.borderWidth = 1;

    myButton.layer.cornerRadius = 6;

    

//    myButton.backgroundColor = [UIColor blueColor];

    

//    myButton2.layer.borderWidth = 1;

//    myButton2.layer.cornerRadius = 6;

//    myButton2.backgroundColor = [UIColor blackColor];

    

    [self.window addSubview:myButton];

//    [self.window addSubview:myButton2];

    

    //修改button的标题

//    [myButton setTitle:@"确认" forState:UIControlStateNormal];

    

    //button添加点击方法

    [myButton addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];

    

    //init创建button没有指定类型

    

//    [myButton2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    

    //button设置图片  //图片名为"check.png"

    [myButton setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];

     //setBackgroundImage方法会把图片铺满整个button

    

    //隐藏多余的部分

    myButton.layer.masksToBounds = YES;

    

    self.isSelected = NO;

    

    UIButton *switchButton = [UIButton buttonWithType:UIButtonTypeCustom];

    switchButton.frame = CGRectMake(300, 300, 50, 50);

    

    //添加图片

    //如果要使用setImage,button类型必须是Custom

    [switchButton setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];

    [self.window addSubview:switchButton];

    [switchButton addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];

    

    

    //用便利构造器创建的button不需要内存管理

//    [myButton2 release];

    [_window release];

    return YES;

}

//换标题

//- (void)click:(UIButton *)button

//{

    NSLog(@"king");

    NSLog(@"%@", button.currentTitle); //按钮的当前标题

//    if ([button.currentTitle isEqualToString:@"确认"]) {

//        [button setTitle:@"取消" forState:UIControlStateNormal];

//    }else

//    {

//        [button setTitle:@"确认" forState:UIControlStateNormal];

//    }

//    

//}


//换图片 相当于多选按钮

//- (void)changeImage:(UIButton *)button

//{

//    if (self.isSelected == NO) {

//        [button setImage:[UIImage imageNamed:@"BtnOn.png"] forState:UIControlStateNormal];

//    }else{

//        [button setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];

//    }

//    //修改状态

//    self.isSelected = !self.isSelected;

//}

// 开关按钮

- (void)changeImage:(UIButton *)button{

    if (self.isSelected ==NO) {

        [button setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];

    }else{

        [button setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];

    }

    self.isSelected = !self.isSelected;

}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值