国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS開發UI篇—xib的簡單使用實例

2019-10-21 18:51:01
字體:
來源:轉載
供稿:網友

這個博客申請了有一段時間了,覺得好像是該寫點什么了。這篇文章主要是關于一些xib的簡單的用法,希望可以幫助到剛剛使用xib的新手們。

什么是xib? xib能做什么?

用來描述軟件界面的文件。

如果沒有xib,所有的界面都需要通過代碼來手動創建。

有了xib以后,可以在xib中進行可視化開發,然后加載xib文件的時候,系統自動生成對應的代碼來創建界面。

與xib類似的還有storyboard文件。xib和storyboard的比較,一個輕量級一個重量級。

共同點:

都用來描述軟件界面。都用Interface Builder工具來編輯

不同點:

Xib是輕量級的,用來描述局部的UI界面

Storyboard是重量級的,用來描述整個軟件的多個界面,并且能展示多個界面之間的跳轉關系

二、xib的簡單使用

1.建立xib文件ios中xib的使用,ios,xib使用教程,ios開發xib的使用
建立的xib文件命名為appxib.xibios中xib的使用,ios,xib使用教程,ios開發xib的使用
2.對xib進行設置

根據程序的需要,這里把view調整為自由布局ios中xib的使用,ios,xib使用教程,ios開發xib的使用
建立view模型(設置長寬等參數)ios中xib的使用,ios,xib使用教程,ios開發xib的使用
調整布局和內部的控件ios中xib的使用,ios,xib使用教程,ios開發xib的使用
完成后的單個view
ios中xib的使用,ios,xib使用教程,ios開發xib的使用

3.使用xib文件的代碼示例

YYViewController.m文件代碼如下:

//// YYViewController.m// 10-xib文件的使用//// Created by apple on 14-5-24.// Copyright (c) 2014年 itcase. All rights reserved.//#import "YYViewController.h"#import "YYapp.h"@interface YYViewController ()@property(nonatomic,strong)NSArray *app;@end@implementation YYViewController//1.加載數據信息-(NSArray *)app{  if (!_app) {    NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];    NSArray *temparray=[NSArray arrayWithContentsOfFile:path];        //字典轉模型    NSMutableArray *arrayM=[NSMutableArray array ];    for (NSDictionary *dict in temparray) {      [arrayM addObject:[YYapp appWithDict:dict]];    }    _app=arrayM;  }  return _app;}//創建界面原型- (void)viewDidLoad{  [super viewDidLoad];  NSLog(@"%d",self.app.count);    //九宮格布局  int totalloc=3;  CGFloat appviewW=80;  CGFloat appviewH=90;  CGFloat margin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+1);    int count=self.app.count;  for (int i=0; i<count; i++) {        int row=i/totalloc;    int loc=i%totalloc;    CGFloat appviewX=margin + (margin +appviewW)*loc;    CGFloat appviewY=margin + (margin +appviewH)*row;    YYapp *app=self.app[i];        //拿出xib視圖    NSArray *apparray= [[NSBundle mainBundle]loadNibNamed:@"appxib" owner:nil options:nil];    UIView *appview=[apparray firstObject];    //加載視圖    appview.frame=CGRectMake(appviewX, appviewY, appviewW, appviewH);        UIImageView *appviewImg=(UIImageView *)[appview viewWithTag:1];    appviewImg.image=app.image;        UILabel *appviewlab=(UILabel *)[appview viewWithTag:2];    appviewlab.text=app.name;        UIButton *appviewbtn=(UIButton *)[appview viewWithTag:3];    [appviewbtn addTarget:self action:@selector(appviewbtnClick:) forControlEvents:UIControlEventTouchUpInside];    appviewbtn.tag=i;        [self.view addSubview:appview];  }}/**按鈕的點擊事件*/-(void)appviewbtnClick:(UIButton *)btn{  YYapp *apps=self.app[btn.tag];  UILabel *showlab=[[UILabel alloc]initWithFrame:CGRectMake(60, 450, 200, 20)];  [showlab setText:[NSString stringWithFormat: @"%@下載成功",apps.name]];  [showlab setBackgroundColor:[UIColor lightGrayColor]];  [self.view addSubview:showlab];  showlab.alpha=1.0;    //簡單的動畫效果  [UIView animateWithDuration:2.0 animations:^{    showlab.alpha=0;  } completion:^(BOOL finished) {    [showlab removeFromSuperview];  }];}@end

運行效果:
ios中xib的使用,ios,xib使用教程,ios開發xib的使用

三、對xib進行連線示例

1.連線示例

新建一個xib對應的視圖類,繼承自Uiviewios中xib的使用,ios,xib使用教程,ios開發xib的使用
在xib界面右上角與新建的視圖類進行關聯ios中xib的使用,ios,xib使用教程,ios開發xib的使用
把xib和視圖類進行連線ios中xib的使用,ios,xib使用教程,ios開發xib的使用
注意:在使用中把weak改成為強引用。否則...

2.連線后的代碼示例

YYViewController.m文件代碼如下:

//// YYViewController.m// 10-xib文件的使用//// Created by apple on 14-5-24.// Copyright (c) 2014年 itcase. All rights reserved.//#import "YYViewController.h"#import "YYapp.h"#import "YYappview.h"@interface YYViewController ()@property(nonatomic,strong)NSArray *app;@end@implementation YYViewController//1.加載數據信息-(NSArray *)app{  if (!_app) {    NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];    NSArray *temparray=[NSArray arrayWithContentsOfFile:path];        //字典轉模型    NSMutableArray *arrayM=[NSMutableArray array ];    for (NSDictionary *dict in temparray) {      [arrayM addObject:[YYapp appWithDict:dict]];    }    _app=arrayM;  }  return _app;}//創建界面原型- (void)viewDidLoad{  [super viewDidLoad];  NSLog(@"%d",self.app.count);    //九宮格布局  int totalloc=3;  CGFloat appviewW=80;  CGFloat appviewH=90;  CGFloat margin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+1);    int count=self.app.count;  for (int i=0; i<count; i++) {        int row=i/totalloc;    int loc=i%totalloc;    CGFloat appviewX=margin + (margin +appviewW)*loc;    CGFloat appviewY=margin + (margin +appviewH)*row;    YYapp *app=self.app[i];        //拿出xib視圖    NSArray *apparray= [[NSBundle mainBundle]loadNibNamed:@"appxib" owner:nil options:nil];        //注意這里的類型名!    //UIView *appview=[apparray firstObject];    YYappview *appview=[apparray firstObject];        //加載視圖    appview.frame=CGRectMake(appviewX, appviewY, appviewW, appviewH);     [self.view addSubview:appview];        appview.appimg.image=app.image;    appview.applab.text=app.name;    appview.appbtn.tag=i;        [ appview.appbtn addTarget:self action:@selector(appviewbtnClick:) forControlEvents:UIControlEventTouchUpInside];      }}/**按鈕的點擊事件*/-(void)appviewbtnClick:(UIButton *)btn{  YYapp *apps=self.app[btn.tag];  UILabel *showlab=[[UILabel alloc]initWithFrame:CGRectMake(60, 450, 200, 20)];  [showlab setText:[NSString stringWithFormat: @"%@下載成功",apps.name]];  [showlab setBackgroundColor:[UIColor lightGrayColor]];  [self.view addSubview:showlab];  showlab.alpha=1.0;    //簡單的動畫效果  [UIView animateWithDuration:2.0 animations:^{    showlab.alpha=0;  } completion:^(BOOL finished) {    [showlab removeFromSuperview];  }];}@end

YYappview.h文件代碼(已經連線)

#import <UIKit/UIKit.h>@interface YYappview : UIView@property (strong, nonatomic) IBOutlet UIImageView *appimg;@property (strong, nonatomic) IBOutlet UILabel *applab;@property (strong, nonatomic) IBOutlet UIButton *appbtn;@end

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东平县| 北辰区| 呼伦贝尔市| 故城县| 金阳县| 岑溪市| 沁水县| 芷江| 湖北省| 黄大仙区| 鹤壁市| 吉林省| 东台市| 泗阳县| 阳西县| 宜兴市| 油尖旺区| 襄樊市| 横山县| 长沙县| 称多县| 兴安盟| 广元市| 惠水县| 桐城市| 天长市| 林甸县| 古蔺县| 开远市| 阳山县| 夏津县| 阿拉善左旗| 通州市| 泰兴市| 忻州市| 岑溪市| 古蔺县| 汝城县| 深圳市| 西平县| 黎川县|