iOS开发动态添加按钮

移动开发 iOS
动态添加按钮想要的效果是,单击一个已有的按钮后自动创建一个新的按钮,并为新按钮添加事件,使得单击时弹出提示框。

新接触iPad开发,把最近用到的记下来,省得以后忘记又要找。

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

1、运行Xcode 4.2,新建一个Single View Application工程,取名DynamicButton:

2、打开ViewController.xib,拖一个按钮到视图,按钮名设置为“添加一个按钮”。

3、选中这个按钮,按住Ctrl,把按钮拖到ViewController.h文件指定位置:

松开鼠标,在弹出的窗口键入以下信息:

即为这个按钮添加响应事件 addButton

4、打开ViewController.m,找到addButton函数,添加以下代码:

  1. - (IBAction)addButton:(id)sender { 
  2.     //动态添加一个按钮 
  3.     CGRect frame = CGRectMake(300, 300, 300, 50);  
  4.     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  5.     button.frame = frame; 
  6.     [button setTitle:@"新添加的动态按钮" forState: UIControlStateNormal];   
  7.     button.backgroundColor = [UIColor clearColor];   
  8.     button.tag = 2000; 
  9.     [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];   
  10.     [self.view addSubview:button];   

5、在addButton函数后边添加一个函数:

  1. //这个是新按钮的响应函数 
  2. -(IBAction) buttonClicked:(id)sender {   
  3.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"  
  4.                                                     message:@"单击了动态按钮!"    
  5.                                                    delegate:self    
  6.                                           cancelButtonTitle:@"确定"   
  7.                                           otherButtonTitles:nil];   
  8.     [alert show]; 
  9.     [alert release]; 

6、编译并运行:

[[70083]]

责任编辑:闫佳明 来源: oschina
相关推荐

2011-03-11 14:07:51

Ubuntu 11.0

2020-05-15 10:22:33

WebFlutter动态化

2013-07-29 05:01:31

iOS开发iOS开发学习按钮拖动和点击

2015-07-23 15:15:06

动态弹出

2011-08-19 15:09:00

IOS开发

2011-07-29 14:55:25

iPhone开发 动画过渡

2010-01-19 10:12:39

VB.NET Butt

2009-08-11 13:27:09

C#动态图像按钮

2015-07-09 15:04:53

JSPatch动态更新ios app

2011-12-25 21:00:30

iPhone

2011-08-01 16:50:28

Xcode 动态 View

2009-12-17 09:56:26

Linux添加驱动模块

2010-05-13 10:19:09

Widget开发

2012-03-31 09:45:08

微软Windows开始按钮

2009-01-04 09:57:24

2011-04-08 13:58:10

Android界面设计

2022-06-27 14:12:32

css鸿蒙自定义

2021-06-25 05:53:09

物联网人工智能机器人

2023-10-18 11:01:07

GNOME按钮

2009-11-25 10:16:49

linuxphpizePHP
点赞
收藏

51CTO技术栈公众号