日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Iphone代码创建视图

發布時間:2023/11/30 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Iphone代码创建视图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

要想以編程的方式創建視圖,需要使用視圖控制器中定義的viewDidLoad方法,只有在運行期間生成UI時才需要實現該方法。

在此只貼出viewDidLoad方法的代碼,因為只需要在這個方法里面編寫代碼:

[cpp] view plaincopyprint?
  • -?(void)viewDidLoad??
  • {??
  • ????self.navigationItem.title?=?@"動態創建UI";??
  • ????UIView?*myview?=?[[UIView?alloc]initWithFrame:[UIScreen?mainScreen].applicationFrame];??
  • ????myview.backgroundColor?=?[UIColor?blackColor];??
  • ??????
  • ????CGRect?frame?=?CGRectMake(10,?15,?300,?20);??
  • ????UILabel?*mylabel?=?[[UILabel?alloc]initWithFrame:frame];??
  • ????mylabel.text?=?@"這是動態創建的label";??
  • ????mylabel.backgroundColor?=?[UIColor?clearColor];??
  • ????mylabel.font?=?[UIFont?fontWithName:@"Verdana"?size:20];??
  • ????mylabel.textColor?=?[UIColor?lightGrayColor];??
  • ????mylabel.textAlignment?=?UITextAlignmentCenter;??
  • ??????
  • ????frame?=?CGRectMake(10,?70,?300,?50);??
  • ????//UIButton?*mybutton?=?[[UIButton?alloc]initWithFrame:frame]; ??
  • ????UIButton?*mybutton?=?[UIButton?buttonWithType:UIButtonTypeRoundedRect];??
  • ????mybutton.frame?=?frame;??
  • ????[mybutton?setTitle:@"點我"?forState:UIControlStateNormal];??
  • ????mybutton.backgroundColor?=?[UIColor?clearColor];??
  • ????[mybutton?addTarget:self?action:@selector(buttonclc)?forControlEvents:UIControlEventTouchUpInside];??
  • ????[myview?addSubview:mylabel];??
  • ????[myview?addSubview:mybutton];??
  • ????self.view?=?myview;??
  • ????[mylabel?release];??
  • ????[super?viewDidLoad];??
  • ????//?Do?any?additional?setup?after?loading?the?view?from?its?nib. ??
  • }??
  • - (void)viewDidLoad {self.navigationItem.title = @"動態創建UI";UIView *myview = [[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];myview.backgroundColor = [UIColor blackColor];CGRect frame = CGRectMake(10, 15, 300, 20);UILabel *mylabel = [[UILabel alloc]initWithFrame:frame];mylabel.text = @"這是動態創建的label";mylabel.backgroundColor = [UIColor clearColor];mylabel.font = [UIFont fontWithName:@"Verdana" size:20];mylabel.textColor = [UIColor lightGrayColor];mylabel.textAlignment = UITextAlignmentCenter;frame = CGRectMake(10, 70, 300, 50);//UIButton *mybutton = [[UIButton alloc]initWithFrame:frame];UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];mybutton.frame = frame;[mybutton setTitle:@"點我" forState:UIControlStateNormal];mybutton.backgroundColor = [UIColor clearColor];[mybutton addTarget:self action:@selector(buttonclc) forControlEvents:UIControlEventTouchUpInside];[myview addSubview:mylabel];[myview addSubview:mybutton];self.view = myview;[mylabel release];[super viewDidLoad];// Do any additional setup after loading the view from its nib. }
    解釋下上面的代碼:

    上面代碼有三段:

    第一段創建的是UIView對象,它可以作為容器容納其他視圖。

    第二段是創建一個Label視圖。

    第三段是創建一個UIButton視圖。

    注意,千萬不要忘記將創建的視圖加入第一步創建的view中,也不要忘記了將創建的view賦值給當前窗口的view:

    [cpp] view plaincopyprint?
  • [myview?addSubview:mylabel];??
  • ????[myview?addSubview:mybutton];??
  • ????self.view?=?myview;??
  • [myview addSubview:mylabel];[myview addSubview:mybutton];self.view = myview;

    總結

    以上是生活随笔為你收集整理的Iphone代码创建视图的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。