UIButton

来源:互联网 发布:中国经济数据公布 编辑:程序博客网 时间:2024/04/25 04:02
- (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
原创粉丝点击