一、动态添加Button
[img]
[/img]
动态添加Button的效果就是点击之后,生成一个按钮,并为按钮添加点击的方法。
1、在xib文件上拖拽添加一个button,标题为:添加button。
2、按住ctrl键拖拽到addbuttonViewController.m文件空白处,生成IBAction,填充代码后如下:
谨记,并注意:fram前面没有 *
哎,犯了好几次这样的小错 我靠,fuck;
- (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点击弹出提示框。
-(void) someButtonClicked{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您点击了动态按钮!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
二、监听UIAlertView
1、在上面的代码基础上,在addbuttonViewController.h文件添加委托
#import <UIKit/UIKit.h>
@interface addbuttonViewController : UIViewController<UIAlertViewDelegate>
- (IBAction)addButton:(id)sender;
@end
2、在AlertView中多添加两个按钮
-(void) someButtonClicked{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您点击了动态按钮!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:@"取消",@"第三项",nil];
[alert show];
}
3、在对应的.m文件中实现委托中的方法
监听你点击了那个按钮
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex:%d", buttonIndex);
}
点击AlertView中弹出的三个按钮打印的结果:
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
- 大小: 13.5 KB
分享到:
相关推荐
1.系统的UIAlertView使用方便,但是有时需要三个按钮,这时的UIAlertView会出现问题,三个按钮的默认摆放位置,不尽人意 2.使用UIAlertView的Delegate,来修改其布局
自定义 UIAlertView
UIAlertView 的替代品
封装体统的UIAlertView,使用Block传递数据。
UIAlertView例子 包括最基础的,和按钮事件,以及带文本输入的Alert
一个简单的封装UIalertView,可以达到展示列表的目的
swift - UIAlertView的使用
iphone中的基础控件UIAlertView,适合初学者,主要学习UIAlertView的常用属性设置、点击事件、添加其他视图到UIAlertView上等
UIAlertView左对齐 UIAlertView左对齐
UIAlertView自动消失,原文地址:http://blog.csdn.net/u010962810/article/details/21245785
NULL 博文链接:https://ikrboy.iteye.com/blog/2002976
JBAlertView为UIAlertView动态添加一个Block属性,将AlertView与button的点击操作关联,简单的区分处理一个视图有多个AlertView弹窗 Usage 初始化AlertView,设置Block #import "UIAlertView+JBAlertView.h" //初始...
因为iOS8 以后UIAlertView已经不推荐使用 所以自己简单封装了一下UIAlertView 和 UIAlertController 根据系统自动选择 封装的比较简单 为了方便使用而已
封装了UIAlertView和UIActionSheet的MessageBox
UIAlertView左对齐且居中显示,兼容>ios7,添加该两个文件,直接调用方法[alertView setTextAlignmentLeft];则可
使用块进行按钮操作的简单UIAlertView包装器。_Obje.zip
https://github.com/STShenZhaoliang/STAlertView 400行写的自定义UIAlertView,没有开太多接口,大家可以自由修改。
1、原理文章:https://kunnan.blog.csdn.net/article/details/113444297 2、应用场景:下单的的付款详情、...3、核心原理:if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])