iOS第五章控件和动作(3)
//通过代码创建一个 Button
roundedButtonType= [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
//设置按钮的位置和尺寸,前两个参数设置 顶点坐标,后两个参数设置 宽、高
roundedButtonType.frame = CGRectMake(50.0, 250.0, 88.0 , 48.0);
//设置按钮的标题和状态
[roundedButtonType setTitle:@"Rounded" forState:UIControlStateNormal];
//设置按钮的背景颜色
roundedButtonType.backgroundColor = [UIColor clearColor];
//设置用户对 按钮进行何种操作,处理按钮事件的操作(回调函数)
[roundedButtonType addTarget:self
action:@selector(action:)
forControlEvents:UIControlEventTouchUpInside];
//设置按钮的编号,便于区分多个按钮
roundedButtonType.tag = 2;
需要单独为按钮事件处理编写方法,一般都叫 xxxAction:
- (void)action:(id)sender
{
//创建一个弹出提示框,
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"提示标题"
message:@"通过纯编码实现的按钮处理方法"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
//显示弹出提示框
[alert show];
}
------------------------------------------------------------------------------------------------------------------------------
自定义按钮
(1)创建带有图片的按钮
[myButton setImage:[UIImageimageNamed:@"myButtonImage.png"]
forState:UIControlStateNormal];
6、控制控件-开关 UISwitch
创建开关
初始化
CGRect switchRect = CGRectMake(120,50,0,0);
UISwitch *mySwitch = [[UISwitch alloc]initWithFrame:switchRect];
添加事件处理
[mySwitch addTarget: self
action:@selector(switchAction:)
forControlEvents: UIControlEventValueChanged];
切换开关
[mySwitch setOn:YES animated:YES];
修改开关外观
获得开关的两个Label
相关新闻>>
- 发表评论
-
- 最新评论 更多>>