iOS学习之动态添加Button和监听UIAlertView按钮
2014-04-06 17:33
591 查看
一、动态添加Button
动态添加Button的效果就是点击之后,生成一个按钮,并为按钮添加点击的方法。
1、在xib文件上拖拽添加一个button,标题为:添加button。
2、按住ctrl键拖拽到addbuttonViewController.m文件空白处,生成IBAction,填充代码后如下:
[cpp] view
plaincopy
- (IBAction)addButton:(id)sender {
CGRect frame = CGRectMake(90, 200, 200, 60);
UIButton *someAddButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
someAddButton.backgroundColor = [UIColor clearColor];
[someAddButton setTitle:@"动态添加一个按钮!" forState:UIControlStateNormal];
someAddButton.frame = frame;
[someAddButton addTarget:self action:@selector(someButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:someAddButton];
}
3、动态生成的button点击事件方法:
生成的button点击弹出提示框。
[cpp] view
plaincopy
-(void) someButtonClicked{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您点击了动态按钮!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
4、编译运行效果 图1 2 3:
图1:
点击按钮后
图2:
图3:
二、监听UIAlertView。
1、在上面的代码基础上,在addbuttonViewController.h文件添加委托
[cpp] view
plaincopy
#import <UIKit/UIKit.h>
@interface addbuttonViewController : UIViewController<UIAlertViewDelegate>
- (IBAction)addButton:(id)sender;
@end
2、在AlertView中多添加两个按钮
[cpp] view
plaincopy
-(void) someButtonClicked{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您点击了动态按钮!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:@"取消",@"第三项",nil];
[alert show];
}
效果图:
3、在对应的.m文件中实现委托中的方法
监听你点击了那个按钮
[cpp] view
plaincopy
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex:%d", buttonIndex);
}
点击AlertView中弹出的三个按钮打印的结果:
[cpp] view
plaincopy
2012-06-14 16:53:18.516 DynamicAddButton[5645:f803] buttonIndex:1
2012-06-14 16:53:23.652 DynamicAddButton[5645:f803] buttonIndex:2
2012-06-14 16:53:25.701 DynamicAddButton[5645:f803] buttonIndex:0
2012-06-14 16:53:39.900 DynamicAddButton[5645:f803] buttonIndex:1
这样你就知道点了按个按钮了。
程序源码下载:gitbub: https://github.com/schelling/YcDemo/tree/master/DynamicAddButton1
csdn资源:http://download.csdn.net/detail/totogo2010/4336287
动态添加Button的效果就是点击之后,生成一个按钮,并为按钮添加点击的方法。
1、在xib文件上拖拽添加一个button,标题为:添加button。
2、按住ctrl键拖拽到addbuttonViewController.m文件空白处,生成IBAction,填充代码后如下:
[cpp] view
plaincopy
- (IBAction)addButton:(id)sender {
CGRect frame = CGRectMake(90, 200, 200, 60);
UIButton *someAddButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
someAddButton.backgroundColor = [UIColor clearColor];
[someAddButton setTitle:@"动态添加一个按钮!" forState:UIControlStateNormal];
someAddButton.frame = frame;
[someAddButton addTarget:self action:@selector(someButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:someAddButton];
}
3、动态生成的button点击事件方法:
生成的button点击弹出提示框。
[cpp] view
plaincopy
-(void) someButtonClicked{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您点击了动态按钮!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
4、编译运行效果 图1 2 3:
图1:
点击按钮后
图2:
图3:
二、监听UIAlertView。
1、在上面的代码基础上,在addbuttonViewController.h文件添加委托
[cpp] view
plaincopy
#import <UIKit/UIKit.h>
@interface addbuttonViewController : UIViewController<UIAlertViewDelegate>
- (IBAction)addButton:(id)sender;
@end
2、在AlertView中多添加两个按钮
[cpp] view
plaincopy
-(void) someButtonClicked{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您点击了动态按钮!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:@"取消",@"第三项",nil];
[alert show];
}
效果图:
3、在对应的.m文件中实现委托中的方法
监听你点击了那个按钮
[cpp] view
plaincopy
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex:%d", buttonIndex);
}
点击AlertView中弹出的三个按钮打印的结果:
[cpp] view
plaincopy
2012-06-14 16:53:18.516 DynamicAddButton[5645:f803] buttonIndex:1
2012-06-14 16:53:23.652 DynamicAddButton[5645:f803] buttonIndex:2
2012-06-14 16:53:25.701 DynamicAddButton[5645:f803] buttonIndex:0
2012-06-14 16:53:39.900 DynamicAddButton[5645:f803] buttonIndex:1
这样你就知道点了按个按钮了。
程序源码下载:gitbub: https://github.com/schelling/YcDemo/tree/master/DynamicAddButton1
csdn资源:http://download.csdn.net/detail/totogo2010/4336287
相关文章推荐
- iOS学习之动态添加Button和监听UIAlertView按钮
- iOS学习之动态添加Button和监听UIAlertView按钮
- iOS学习基础学习之动态添加Button和监听UIAlertView按钮
- iOS学习之动态添加Button和监听UIAlertView按钮
- IOS学习之动态添加Button和监听UIAlertView按钮
- IOS学习之动态添加Button和监听UIAlertView按钮
- iOS学习之动态添加Button和监听UIAlertView按钮
- IOS学习1 动态添加Button
- IOS学习之UINavigationController详解与使用:添加UIBarButtonItem (转)
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
- android学习——使用TableLayout动态生成表格,并为tablerow中的列添加点击监听
- ios开发runtime学习三:动态添加方法(实际应用少,面试)
- Jquery学习-用Jquery添加一个按扭后无法动态对该按扭实行监听解决方法
- IOS学习之UINavigationController详解与使用:添加UIBarButtonItem
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
- Android开发学习笔记:为动态添加的EditText控件绑定值改变监听事件
- (转载)IOS学习之UINavigationController详解与使用:添加UIBarButtonItem