//
//
ViewController.m
//
DynamicButton
//
//
Created
by
cyx
on
13-3-4.
//
Copyright
(c)
2013年
cyx.
All
rights
reserved.
//
#import
"ViewController.h"
@interface
ViewController
()
@end
@implementation
ViewController
-
(void)viewDidLoad
{
[super
viewDidLoad];
//
Do
any
additional
setup
after
loading
the
view,
typically
from
a
nib.
}
-
(void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
//
Dispose
of
any
resources
that
can
be
recreated.
}
-
(IBAction)addButton:(id)sender
{
CGRect
frame
=
CGRectMake(300,
300,
300,
50);//定义一个矩形区域,坐标为300,300,宽:300,高:50
UIButton
*button
=
[UIButton
buttonWithType:UIButtonTypeRoundedRect];
//创建一个圆角的矩形
button.frame
=
frame;//给定button在view上的位置
[button
setTitle:@"新添加一个按钮"
forState:UIControlStateNormal];
//设置button标题,forState表示在何种情况下显现,常规状态显现
button.backgroundColor
=
[UIColor
clearColor];
//去除背景色
button.tag
=
2000;
//标记,如果想把一个窗口的所有button分成集中类型,那么不同button的tag属性分别设置成一个,就可以对tag不同的button分别操作了
[button
addTarget:self
action:@selector(buttonClicked)
forControlEvents:(UIControlEventTouchUpIns
ide)];
//跟web的click事件一样,addaddTarget:self的意思是说,在这个方法在本类中也可以传入其他类的指针,当摁下的时候调用buttonClicked方法,UIControlEventTouchUpIns
ide是指在按钮按下时响应
[self.view
addSubview:button];
//显示控件
}
-
(IBAction)buttonClicked:(id)sender
{
UIAlertView
*alert
=
[[UIAlertView
alloc]
initWithTitle:@"提示"
message:@"单击了动态按钮"
delegate:self
cancelButtonTitle:@"sure"
otherButtonTitles:nil,
nil];
[alert
show];
[alert
release];
}
@end
原地址:http://my.oschina.net/plumsoft/blog?catalog=145903&p=1