ios uialertview 上可以添加button吗
推荐于2016-10-22 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517190
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
一、动态添加Button
动态添加Button的效果就是点击之后,生成一个按钮,并为按钮添加点击的方法。
1、在xib文件上拖拽添加一个button,标题为:添加button。
2、按住ctrl键拖拽到addbuttonViewController.m文件空白处,生成IBAction,填充代码后如下:
[cpp] view plaincopyprint?
01.- (IBAction)addButton:(id)sender {
02. CGRect frame = CGRectMake(90, 200, 200, 60);
03. UIButton *someAddButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
04. someAddButton.backgroundColor = [UIColor clearColor];
05. [someAddButton setTitle:@"动态添加一个按钮!" forState:UIControlStateNormal];
06. someAddButton.frame = frame;
07. [someAddButton addTarget:self action:@selector(someButtonClicked) forControlEvents:UIControlEventTouchUpInside];
08. [self.view addSubview:someAddButton];
09.}
- (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 plaincopyprint?
01.-(void) someButtonClicked{
02. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
03. message:@"您点击了动态按钮!"
04. delegate:self
05. cancelButtonTitle:@"确定"
06. otherButtonTitles:nil];
07. [alert show];
08.}
-(void) someButtonClicked{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您点击了动态按钮!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
4、编译运行效果
点击按钮后
二、监听UIAlertView。
1、在上面的代码基础上,在addbuttonViewController.h文件添加委托
[cpp] view plaincopyprint?
01.#import <UIKit/UIKit.h>
02.
03.@interface addbuttonViewController : UIViewController<UIAlertViewDelegate>
04.- (IBAction)addButton:(id)sender;
05.
06.@end
#import <UIKit/UIKit.h>
@interface addbuttonViewController : UIViewController<UIAlertViewDelegate>
- (IBAction)addButton:(id)sender;
@end
2、在AlertView中多添加两个按钮
[cpp] view plaincopyprint?
01.-(void) someButtonClicked{
02. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
03. message:@"您点击了动态按钮!"
04. delegate:self
05. cancelButtonTitle:@"确定"
06. otherButtonTitles:@"取消",@"第三项",nil];
07. [alert show];
08.}
-(void) someButtonClicked{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您点击了动态按钮!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:@"取消",@"第三项",nil];
[alert show];
}
效果图:
3、在对应的.m文件中实现委托中的方法
监听你点击了那个按钮
[cpp] view plaincopyprint?
01.-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
02.{
03. NSLog(@"buttonIndex:%d", buttonIndex);
04.}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex:%d", buttonIndex);
}点击AlertView中弹出的三个按钮打印的结果:
[cpp] view plaincopyprint?
01.2012-06-14 16:53:18.516 DynamicAddButton[5645:f803] buttonIndex:1
02.2012-06-14 16:53:23.652 DynamicAddButton[5645:f803] buttonIndex:2
03.2012-06-14 16:53:25.701 DynamicAddButton[5645:f803] buttonIndex:0
04.2012-06-14 16:53:39.900 DynamicAddButton[5645:f803] buttonIndex:1
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
这样你就知道点了按个按钮了。
动态添加Button的效果就是点击之后,生成一个按钮,并为按钮添加点击的方法。
1、在xib文件上拖拽添加一个button,标题为:添加button。
2、按住ctrl键拖拽到addbuttonViewController.m文件空白处,生成IBAction,填充代码后如下:
[cpp] view plaincopyprint?
01.- (IBAction)addButton:(id)sender {
02. CGRect frame = CGRectMake(90, 200, 200, 60);
03. UIButton *someAddButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
04. someAddButton.backgroundColor = [UIColor clearColor];
05. [someAddButton setTitle:@"动态添加一个按钮!" forState:UIControlStateNormal];
06. someAddButton.frame = frame;
07. [someAddButton addTarget:self action:@selector(someButtonClicked) forControlEvents:UIControlEventTouchUpInside];
08. [self.view addSubview:someAddButton];
09.}
- (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 plaincopyprint?
01.-(void) someButtonClicked{
02. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
03. message:@"您点击了动态按钮!"
04. delegate:self
05. cancelButtonTitle:@"确定"
06. otherButtonTitles:nil];
07. [alert show];
08.}
-(void) someButtonClicked{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您点击了动态按钮!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
4、编译运行效果
点击按钮后
二、监听UIAlertView。
1、在上面的代码基础上,在addbuttonViewController.h文件添加委托
[cpp] view plaincopyprint?
01.#import <UIKit/UIKit.h>
02.
03.@interface addbuttonViewController : UIViewController<UIAlertViewDelegate>
04.- (IBAction)addButton:(id)sender;
05.
06.@end
#import <UIKit/UIKit.h>
@interface addbuttonViewController : UIViewController<UIAlertViewDelegate>
- (IBAction)addButton:(id)sender;
@end
2、在AlertView中多添加两个按钮
[cpp] view plaincopyprint?
01.-(void) someButtonClicked{
02. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
03. message:@"您点击了动态按钮!"
04. delegate:self
05. cancelButtonTitle:@"确定"
06. otherButtonTitles:@"取消",@"第三项",nil];
07. [alert show];
08.}
-(void) someButtonClicked{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您点击了动态按钮!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:@"取消",@"第三项",nil];
[alert show];
}
效果图:
3、在对应的.m文件中实现委托中的方法
监听你点击了那个按钮
[cpp] view plaincopyprint?
01.-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
02.{
03. NSLog(@"buttonIndex:%d", buttonIndex);
04.}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex:%d", buttonIndex);
}点击AlertView中弹出的三个按钮打印的结果:
[cpp] view plaincopyprint?
01.2012-06-14 16:53:18.516 DynamicAddButton[5645:f803] buttonIndex:1
02.2012-06-14 16:53:23.652 DynamicAddButton[5645:f803] buttonIndex:2
03.2012-06-14 16:53:25.701 DynamicAddButton[5645:f803] buttonIndex:0
04.2012-06-14 16:53:39.900 DynamicAddButton[5645:f803] buttonIndex:1
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
这样你就知道点了按个按钮了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询