ios入门第一课


@interface ViewController (){
    //设置便签
    BOOL _btn;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //定义并初始化一个按钮
    UIButton *btn =[[UIButton alloc]init];
    // 设置按钮的位置和长宽
     [btn setFrame:CGRectMake(160.0, 274, 100, 100)];
    //设置初始颜色
    [btn setBackgroundColor:[UIColor redColor]];
    //绑定点击事件
     [btn addTarget:self action:@selector(btnclick:)forControlEvents:UIControlEventTouchUpInside];
    //把按钮添加到视图中
    [self.view addSubview:btn];

}
- (void)btnclick:(UIButton *)button {
    button.selected = !(_btn);
    if (button.selected) {
        button.backgroundColor = [UIColor blueColor];
        _btn = YES;
    } else {
        button.backgroundColor = [UIColor redColor];
        _btn = NO;
    }
}

你可能感兴趣的:(ios学习)