自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS開發(fā)動(dòng)態(tài)添加按鈕

移動(dòng)開發(fā) iOS
動(dòng)態(tài)添加按鈕想要的效果是,單擊一個(gè)已有的按鈕后自動(dòng)創(chuàng)建一個(gè)新的按鈕,并為新按鈕添加事件,使得單擊時(shí)彈出提示框。

新接觸iPad開發(fā),把最近用到的記下來,省得以后忘記又要找。

想要的效果是,單擊一個(gè)已有的按鈕后自動(dòng)創(chuàng)建一個(gè)新的按鈕,并為新按鈕添加事件,使得單擊時(shí)彈出提示框。

1、運(yùn)行Xcode 4.2,新建一個(gè)Single View Application工程,取名DynamicButton:

2、打開ViewController.xib,拖一個(gè)按鈕到視圖,按鈕名設(shè)置為“添加一個(gè)按鈕”。

3、選中這個(gè)按鈕,按住Ctrl,把按鈕拖到ViewController.h文件指定位置:

松開鼠標(biāo),在彈出的窗口鍵入以下信息:

即為這個(gè)按鈕添加響應(yīng)事件 addButton

4、打開ViewController.m,找到addButton函數(shù),添加以下代碼:

  1. - (IBAction)addButton:(id)sender { 
  2.     //動(dòng)態(tài)添加一個(gè)按鈕 
  3.     CGRect frame = CGRectMake(300, 300, 300, 50);  
  4.     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  5.     button.frame = frame; 
  6.     [button setTitle:@"新添加的動(dòng)態(tài)按鈕" 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函數(shù)后邊添加一個(gè)函數(shù):

  1. //這個(gè)是新按鈕的響應(yīng)函數(shù) 
  2. -(IBAction) buttonClicked:(id)sender {   
  3.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"  
  4.                                                     message:@"單擊了動(dòng)態(tài)按鈕!"    
  5.                                                    delegate:self    
  6.                                           cancelButtonTitle:@"確定"   
  7.                                           otherButtonTitles:nil];   
  8.     [alert show]; 
  9.     [alert release]; 

6、編譯并運(yùn)行:

[[70083]]

責(zé)任編輯:閆佳明 來源: oschina
相關(guān)推薦

2011-03-11 14:07:51

Ubuntu 11.0

2020-05-15 10:22:33

WebFlutter動(dòng)態(tài)化

2013-07-29 05:01:31

iOS開發(fā)iOS開發(fā)學(xué)習(xí)按鈕拖動(dòng)和點(diǎn)擊

2015-07-23 15:15:06

動(dòng)態(tài)彈出

2010-01-19 10:12:39

VB.NET Butt

2011-07-29 14:55:25

iPhone開發(fā) 動(dòng)畫過渡

2011-08-19 15:09:00

IOS開發(fā)

2009-08-11 13:27:09

C#動(dòng)態(tài)圖像按鈕

2011-12-25 21:00:30

iPhone

2015-07-09 15:04:53

JSPatch動(dòng)態(tài)更新ios app

2009-12-17 09:56:26

Linux添加驅(qū)動(dòng)模塊

2011-08-01 16:50:28

Xcode 動(dòng)態(tài) View

2011-04-08 13:58:10

Android界面設(shè)計(jì)

2010-05-13 10:19:09

Widget開發(fā)

2012-03-31 09:45:08

微軟Windows開始按鈕

2009-01-04 09:57:24

2022-06-27 14:12:32

css鴻蒙自定義

2021-06-25 05:53:09

物聯(lián)網(wǎng)人工智能機(jī)器人

2023-05-26 00:14:38

PythonJinJa2

2010-04-14 12:51:04

Oracle動(dòng)態(tài)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)