首页 > 移动平台 > 详细

iOS 动态添加按钮

时间:2014-11-05 14:52:53      阅读:365      评论:0      收藏:0      [点我收藏+]

单击一个已有的按钮后自动创建一个新的按钮,并为新按钮添加事件,使得单击时弹出提示框。

在viewcontroller.h中添加

@property (weak, nonatomic) IBOutlet UIButton *addbutton;

为这个按钮添加响应事件addbutton

在viewcontroller.m中添加


- (IBAction)addButton:(id)sender {

    //动态添加一个按钮

    CGRect frame = CGRectMake(0, 0, 300, 50);

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button.frame = frame;

    [button setTitle:@"新添加的动态按钮" forState: UIControlStateNormal];

    button.backgroundColor = [UIColor redColor];

    button.tag = 2000;

    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

}

//这个是新按钮的响应函数

-(IBAction) buttonClicked:(id)sender {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"

                                                    message:@"单击了动态按钮!"

                                                   delegate:self

                                          cancelButtonTitle:@"确定"

                                          otherButtonTitles:nil];

    [alert show];

}



iOS 动态添加按钮

原文:http://blog.csdn.net/jichunw/article/details/40821717

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!