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

Xcode學習筆記之動態(tài)添加View

移動開發(fā) iOS
本文介紹的是Xcode學習筆記之動態(tài)添加View,詳細介紹了動態(tài)添加View的實例,先來看內(nèi)容詳解。

Xcode學習筆記之動態(tài)添加View是本文要介紹的內(nèi)容,前面說的都是用的Interface Builder來編輯.xib文件來給窗口添加各種件以及給控件綁定數(shù)據(jù)(IBOutlet)、關聯(lián)事件響應函數(shù)(IBAction)。這章學習的是動態(tài)的添加view,不使用Interface Builder。這里用label和button示例:

找到新建工程XXXViewController.m的-(void)loadView方法,去掉注釋并添加如下代碼

  1. - (void)loadView {  
  2. //創(chuàng)建一個UIView 對象  
  3. UIView *view =  
  4. [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];  
  5. view.backgroundColor = [UIColor lightGrayColor];  
  6. //創(chuàng)建一個label view  
  7. CGRect frame = CGRectMake(10, 15, 300, 20);  
  8. UILabel *label = [[UILabel alloc] initWithFrame:frame];  
  9. label.textAlignment = UITextAlignmentCenter;  
  10. label.backgroundColor = [UIColor clearColor];  
  11. label.font = [UIFont fontWithName:@”Verdana” size:20];  
  12. label.text = @”label test”;  
  13. label.tag = 1000;  
  14. //創(chuàng)建一個按鈕view  
  15. frame = CGRectMake(10, 30, 300, 50);  
  16. UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  17. button.frame = frame;  
  18. [button setTitle:@”button test” forState:UIControlStateNormal];  
  19. button.backgroundColor = [UIColor clearColor];  
  20. button.tag = 2000

/*下面這個調(diào)用用C++的格式來看就是

  1. button->addTarget(this->action, @selector(buttonClicked:), UIControlEventTouchUpInside); 

中間的action:以及forControlEvent:實際上都是函數(shù)簽名的一部分。@selector(buttonClicked:) 相當于函數(shù)指針(一個冒號表明函數(shù)有一個參數(shù)),這里指向的是buttonClicked函數(shù)

也就是下面定義的按鈕響應函數(shù)*/

  1. [button addTarget:self action:@selector(buttonClicked:) forControlEvent:UIControlEventTouchUpInside];  
  2. [view addSubview:label];  
  3. [view addSubview:button];  
  4. self.view = view;  
  5. [label release];  

在這個文件中添加按鈕響應函數(shù)

  1. -(IBAtion) buttonClicked:(id)sender {  
  2. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Action invoked”   
  3. message:@”button clicked”   
  4. delegate:self   
  5. cancelButtonTitle:”@ok”   
  6. otherButtonTitles:nil];  
  7. [alert show];  
  8. [alert release];  

label的矩形區(qū)域是CGRectMake(10, 15, 300, 20); 既左上角坐標是10,15寬度高度分別是300, 20.

button的矩形區(qū)域的左上角坐標是10, 30 ,它們有重疊的地方。

這里遮擋是后加到view里面去的遮擋先加進去的。所以button遮擋了label。可以通過

  1. [view exchangeSubviewAtIndex:1 withSubviewAtIndex:0]; 

來修改遮擋。我的理解是view按照控件加進去的順給了個index,這個index從0開始遞增。顯示的時候index數(shù)值較大控件遮擋數(shù)值較小的。 上面這個函數(shù)交換了***加進去的兩個控件(實際上只有這兩個)的index。

小結:Xcode學習筆記之動態(tài)添加View的內(nèi)容介紹完了,希望本文對你有所幫助!

責任編輯:zhaolei 來源: 博客園
相關推薦

2011-08-01 17:01:02

Xcode WindowBase View

2011-07-08 17:35:14

Xcode View

2011-08-10 14:00:22

XcodeUIWebView視頻

2011-08-19 15:16:41

XCodeUserScripts腳本

2011-07-25 15:42:38

Xcode Vim

2011-07-20 14:31:56

XCode User Scrip 腳本

2011-08-30 16:43:46

MTK開發(fā)菜單

2011-08-01 15:57:58

2011-08-01 10:13:46

Xcode 視圖 動畫

2011-08-01 10:01:12

Xcode UIView 動畫

2009-09-17 16:20:43

Linq to sql

2009-12-14 15:33:50

動態(tài)路由協(xié)議

2011-08-01 17:50:28

Xcode

2009-12-14 17:56:25

Linux操作系統(tǒng)

2011-03-08 16:30:40

Proftpd

2011-03-08 16:30:24

Proftpd

2010-06-30 10:30:29

UML動態(tài)建模

2011-08-11 16:31:08

XCode

2011-08-18 10:17:21

Xcode4Xcode

2011-08-22 11:35:07

Xcode
點贊
收藏

51CTO技術棧公眾號