首页 > 代码库 > UI学习第二篇 (控件)

UI学习第二篇 (控件)

UIbutton 也是一个控件,它属于UIControl 用的最多的就是事件响应

1.

 //创建按钮对象

  UIButton * _botton = [UIButton buttonWithType:UIButtonTypeCustom];

//设置标题

 [_botton setTitle:@"按住说话" forstate:UIControlStateNormal];

 [_botton setTitle:@"松开说话" forstate:UIControlStateHighlighted];//通常这两句可以只写一个,写两个表示的是两种状态

//给按钮设置背景颜色

 _botton.backgroundColor = [UIColor whiteColor];

//设置按钮的标题颜色

   //[_doneButton setTitleColor:[UIColor whiteColor]];

    

 2.  //给按钮添加相应方法

    [_doneButton addTarget:selfaction:@selector(doneButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    //添加到greeenview 上显示

    [greenView addSubview:_doneButton];

 

3.按钮的响应事件处理方法 ,这里是 doneButtonAction:  另外因为按钮加到了greenview上了,

 

- (void)doneButtonAction:(id)sender{

    NSLog(@"%s",__FUNCTION__);

    NSLog(@"%@",sender);

    //移除Button的响应方法

    [sender removeTarget:selfaction:@selector(doneButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    UITextField *_textFiled = (UITextField *)[self.windowviewWithTag:123];

    // 释放输入框的第一响应者权限

    [_textFiled resignFirstResponder];//点击按钮键盘会被回收

   

//    UITextField * _textFiled = (UITextField *)[self.window viewWithTag:123];

//    [_textFiled resignFirstResponder];

}

 

 

 

 

 

4.创建带图片的按钮

 //重新创建带图片的按钮

    UIButton * _cameraButton =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    _cameraButton.frame = CGRectMake(135, 320, 50, 50);

    [_cameraButton setBackgroundImage:[UIImageimageNamed:@"photo1"] forState:UIControlStateNormal];

    

    [self.window addSubview:_cameraButton];