iOS学习之动态添加Button和监听UIAlertView按钮

转自:https://i-blog.csdnimg.cn/blog_migrate/f5bdac07997d144a4ee1c81ec100bff6.png


一、动态添加Button

动态添加Button的效果就是点击之后,生成一个按钮,并为按钮添加点击的方法。

1、在xib文件上拖拽添加一个button,标题为:添加button。




2、按住ctrl键拖拽到addbuttonViewController.m文件空白处,生成IBAction,填充代码后如下

[cpp]  view plain copy
  1. - (IBAction)addButton:(id)sender {  
  2.     CGRect frame = CGRectMake(90, 200, 200, 60);  
  3.     UIButton *someAddButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  4.     someAddButton.backgroundColor = [UIColor clearColor];  
  5.     [someAddButton setTitle:@"动态添加一个按钮!" forState:UIControlStateNormal];  
  6.     someAddButton.frame = frame;  
  7.     [someAddButton addTarget:self action:@selector(someButtonClicked) forControlEvents:UIControlEventTouchUpInside];  
  8.     [self.view addSubview:someAddButton];  
  9. }  

3、动态生成的button点击事件方法:

生成的button点击弹出提示框。

[cpp]  view plain copy
  1. -(void) someButtonClicked{    
  2.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"   
  3.                                                     message:@"您点击了动态按钮!"     
  4.                                                    delegate:self     
  5.                                           cancelButtonTitle:@"确定"    
  6.                                           otherButtonTitles:nil];    
  7.     [alert show];  
  8. }  


4、编译运行效果 图1 2 3:

图1:




点击按钮后

图2:




图3:



二、监听UIAlertView。

1、在上面的代码基础上,在addbuttonViewController.h文件添加委托

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface addbuttonViewController : UIViewController<UIAlertViewDelegate>  
  4. - (IBAction)addButton:(id)sender;  
  5.   
  6. @end  


2、在AlertView中多添加两个按钮

[cpp]  view plain copy
  1. -(void) someButtonClicked{    
  2.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"   
  3.                                                     message:@"您点击了动态按钮!"     
  4.                                                    delegate:self     
  5.                                           cancelButtonTitle:@"确定"    
  6.                                           otherButtonTitles:@"取消",@"第三项",nil];    
  7.     [alert show];  
  8. }  

效果图:


3、在对应的.m文件中实现委托中的方法

监听你点击了那个按钮

[cpp]  view plain copy
  1. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  2. {  
  3.     NSLog(@"buttonIndex:%d", buttonIndex);  
  4. }  
点击AlertView中弹出的三个按钮打印的结果:

[cpp]  view plain copy
  1. 2012-06-14 16:53:18.516 DynamicAddButton[5645:f803] buttonIndex:1  
  2. 2012-06-14 16:53:23.652 DynamicAddButton[5645:f803] buttonIndex:2  
  3. 2012-06-14 16:53:25.701 DynamicAddButton[5645:f803] buttonIndex:0  
  4. 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值