1、创建按钮的两种方法:
·1、动态创建
btnfont = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnfont setFrame:CGRectMake(100, 10, 120, 40)];
[btnfont addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[btnfont setTitle:@"字体" forState:UIControlStateNormal];
btnfont.backgroundColor=[UIColor clearColor];
[self.view addSubview:btnfont];
·2、在xib文件中已经创建好,通过tag获取按钮
UIButton *testButton= (UIButton*)[self.view viewWithTag:100];
[testButton addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
2、
addAlarmBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[addAlarmBtn setFrame:CGRectMake(0, 892, 394, 71)];
[addAlarmBtn setTitle:@"添加闹钟" forState:UIControlStateNormal];
addAlarmBtn.titleLabel.font = [UIFont systemFontOfSize:26.0];//设置按钮上字体的大小
[addAlarmBtn addTarget:self
action:@selector(addAlarm:)
forControlEvents:UIControlEventTouchUpInside];
addAlarmBtn.backgroundColor = [UIColor yellowColor];
addAlarmBtn.alpha = 0.7f;
[self.view addSubview:addAlarmBtn];
本文转自编程小翁博客园博客,原文链接:http://www.cnblogs.com/wengzilin/archive/2012/03/18/2404587.html,如需转载请自行联系原作者