iOS开发1:动态添加按钮
来源:互联网 发布:许继和南瑞 知乎 编辑:程序博客网 时间:2024/05/16 11:09
想要的效果是,单击一个已有的按钮后自动创建一个新的按钮,并为新按钮添加事件,使得单击时弹出提示框。
1、运行Xcode 4.2,新建一个Single View Application工程,取名DynamicButton:
2、打开ViewController.xib,拖一个按钮到视图,按钮名设置为“添加一个按钮”。
3、选中这个按钮,按住Ctrl,把按钮拖到ViewController.h文件指定位置:
松开鼠标,在弹出的窗口键入以下信息:
即为这个按钮添加响应事件 addButton
4、打开ViewController.m,找到addButton函数,添加以下代码:
01
- (IBAction)addButton:(id)sender {
02
//动态添加一个按钮
03
CGRect frame = CGRectMake(300, 300, 300, 50);
04
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
05
button.frame = frame;
06
[button setTitle:@
"新添加的动态按钮"
forState: UIControlStateNormal];
07
button.backgroundColor = [UIColor clearColor];
08
button.tag = 2000;
09
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
10
[self.view addSubview:button];
11
}
01
//这个是新按钮的响应函数
02
-(IBAction) buttonClicked:(id)sender {
03
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@
"提示"
04
message:@
"单击了动态按钮!"
05
delegate:self
06
cancelButtonTitle:@
"确定"
07
otherButtonTitles:nil];
08
[alert show];
09
[alert release];
10
}
- iOS开发1:动态添加按钮
- iOS开发:动态添加按钮
- iOS开发:动态添加按钮
- iOS 动态添加按钮
- IOS AlertView动态添加按钮
- iOS虚拟键盘上添加动态按钮
- iOS虚拟键盘上添加动态按钮
- iOS: 学习笔记, 动态添加按钮
- 【代码笔记】iOS-UIActionSheet动态添加按钮
- iPhone开发之动态添加UIActionSheet按钮
- iPhone开发之动态添加UIActionSheet按钮
- iOS学习之动态添加Button和监听UIAlertView按钮
- 在iOS虚拟键盘上添加动态隐藏按钮
- iOS学习之动态添加Button和监听UIAlertView按钮
- 在iOS虚拟键盘上添加动态隐藏按钮
- 在iOS虚拟键盘上添加动态隐藏按钮
- iOS学习之动态添加Button和监听UIAlertView按钮
- iOS学习之动态添加Button和监听UIAlertView按钮
- 零基础小白JAVA学习笔记(十)
- 计算机相关书籍推荐(转载)
- 设计一个只能在堆上或栈上实例化的类
- 那些年,mac pro升级lion的那些事
- Android OpenGL ES 教程 第二章 -- 建立多边形
- iOS开发1:动态添加按钮
- [C++学习] C++ Primer 第4版 习题4.28 C++动态数组
- 深入理解Java的迭代器Iterator
- C++构造函数与多线程问题
- jsp內置對象
- iOS开发2:读取plist文件
- 外观模式(Facade)
- iOS开发3:关闭键盘
- 谈AJAX技术在PHP开发中的简单应用-AJAX编程教程