欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS開發(fā)UI篇—xib的簡單使用實例

 更新時間:2016年11月03日 16:40:35   作者:文頂頂  
本篇文章主要介紹了iOS開發(fā)UI篇—xib的簡單使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

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

什么是xib? xib能做什么?

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

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

有了xib以后,可以在xib中進行可視化開發(fā),然后加載xib文件的時候,系統(tǒng)自動生成對應(yīng)的代碼來創(chuàng)建界面。

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

共同點:

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

不同點:

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

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

二、xib的簡單使用

1.建立xib文件
建立的xib文件命名為appxib.xib
2.對xib進行設(shè)置

根據(jù)程序的需要,這里把view調(diào)整為自由布局
建立view模型(設(shè)置長寬等參數(shù))
調(diào)整布局和內(nèi)部的控件
完成后的單個view

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.加載數(shù)據(jù)信息
-(NSArray *)app
{
  if (!_app) {
    NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
    NSArray *temparray=[NSArray arrayWithContentsOfFile:path];
    
    //字典轉(zhuǎn)模型
    NSMutableArray *arrayM=[NSMutableArray array ];
    for (NSDictionary *dict in temparray) {
      [arrayM addObject:[YYapp appWithDict:dict]];
    }
    _app=arrayM;
  }
  return _app;
}

//創(chuàng)建界面原型
- (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

運行效果:

三、對xib進行連線示例

1.連線示例

新建一個xib對應(yīng)的視圖類,繼承自Uiview
在xib界面右上角與新建的視圖類進行關(guān)聯(lián)
把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.加載數(shù)據(jù)信息
-(NSArray *)app
{
  if (!_app) {
    NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
    NSArray *temparray=[NSArray arrayWithContentsOfFile:path];
    
    //字典轉(zhuǎn)模型
    NSMutableArray *arrayM=[NSMutableArray array ];
    for (NSDictionary *dict in temparray) {
      [arrayM addObject:[YYapp appWithDict:dict]];
    }
    _app=arrayM;
  }
  return _app;
}

//創(chuàng)建界面原型
- (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文件代碼(已經(jīng)連線)

#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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • ios7中UIViewControllerBasedStatusBarAppearance作用詳解

    ios7中UIViewControllerBasedStatusBarAppearance作用詳解

    這篇文章主要介紹了 ios7中UIViewControllerBasedStatusBarAppearance作用詳解的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • IOS App圖標和啟動畫面尺寸詳細介紹

    IOS App圖標和啟動畫面尺寸詳細介紹

    這篇文章主要介紹了IOS App圖標和啟動畫面尺寸詳細介紹的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • iOS tableView實現(xiàn)頭部拉伸并改變導(dǎo)航條漸變色

    iOS tableView實現(xiàn)頭部拉伸并改變導(dǎo)航條漸變色

    這篇文章主要為大家詳細介紹了iOS tableView實現(xiàn)頭部拉伸并改變導(dǎo)航條漸變色,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • iOS WKWebView無法處理URL Scheme和App Store鏈接的問題解決

    iOS WKWebView無法處理URL Scheme和App Store鏈接的問題解決

    這篇文章主要給大家介紹了關(guān)于iOS WKWebView無法處理URL Scheme和App Store鏈接的問題解決的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • 淺談iOS解析HTMl標簽以及開發(fā)中的一些坑

    淺談iOS解析HTMl標簽以及開發(fā)中的一些坑

    這篇文章主要介紹了淺談iOS解析HTMl標簽以及開發(fā)中的一些坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • IOS中獲取本地通訊錄聯(lián)系人以及漢字首字母排序

    IOS中獲取本地通訊錄聯(lián)系人以及漢字首字母排序

    這篇文章主要介紹了IOS中獲取本地通訊錄聯(lián)系人以及漢字首字母排序的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • 比較IOS開發(fā)中常用視圖的四種切換方式

    比較IOS開發(fā)中常用視圖的四種切換方式

    這篇文章給大家介紹了在IOS開發(fā)中常用視圖的四種切換方式以及這四種方式的優(yōu)缺點,這四種方式包括:push,pop、modal、切換窗口的根控制器和添加子視圖,有需要的可以參考借鑒。
    2016-08-08
  • iOS查找私有API的方法示例

    iOS查找私有API的方法示例

    這篇文章主要介紹了iOS查找私有API的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • UITableViewCell在編輯狀態(tài)下背景顏色的修改方法

    UITableViewCell在編輯狀態(tài)下背景顏色的修改方法

    這篇文章主要給大家介紹了關(guān)于UITableViewCell在編輯狀態(tài)下背景顏色的修改方法,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • iOS Moya實現(xiàn)OAuth請求的方法

    iOS Moya實現(xiàn)OAuth請求的方法

    這篇文章主要介紹了iOS Moya實現(xiàn)OAuth請求的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12

最新評論