iOS-xib的使用1,iOS-xib使用1

來源:互聯網
上載者:User

iOS-xib的使用1,iOS-xib使用1

一、File‘s owner的解析過程和使用:

1.

  storyboard:描述軟體介面;iOS5.0後出來的。

  xib:描述軟體介面;是storyboard前身。

 

2.

  項目環境裡面的所有資源都要通過 [NSBundle mainBundle]來訪問, 比如訪問圖片的全路徑;

  ====有一個方法:loadNibNamed:(NSString*) owner:(id) option:(NSDictionary*) ===

 

3. 如果xib中的某一個控制項想和控制器進行連線的話:

  1> xib的file‘s owner必須要設定class為相應的控制器;

  2> loadNibNamed:(NSString*) owner:(id) option:(NSDictionary*)中的owner要設定成相應的控制器(因為這個參數不傳的話,啟動並執行時候file‘s owner還是為空白);

  3> xib設定file‘s owner的class,到時候xib裡面的控制項才可以在file’s owner(類或對象,比如Dog對象)中能找到相應的方法去調用。

 

4. xib的本質就是xml,解析示意過程如下:

     

------------------------------------------------------------------------------------------------------------------------------------

5. File‘s owner使用步驟:

    

 

     注:  類型匹配就是說: 在File‘s owner的class中設定的類型要和loadNibNamed:(NSString*) owner option:(NSDictionary*)中的owner參數的類型要一致。

 

二、view的封裝 (自訂view)    

前言:不應該用控制器來監聽xib中按鈕的點擊,因為xib和控制器綁定死了,耦合性太強,不利於擴充重用。

--------------------------------------------------------------------------------------------------------------------------------------------------------------

為方便查看,簡單描述一下通過xib自訂view的概況:

 

=======RowView.m 和 RowView.h 檔案內容 以及xib檔案======

#import "RowView.h"
@implementation RowView
+ (id)rowViewWithIcon:(NSString *)icon name:(NSString *)name
{
    RowView *view = [[NSBundle mainBundle] loadNibNamed:@"RowView" owner:nil options:nil][0];   

#if 1 // 通過tag來拿出xib中的控制項

    // 1.設定表徵圖
    UIButton *iconBtn = (UIButton *)[view viewWithTag:1];
    [view.iconBtn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];  // 註:類方法不能訪問成員變數!所以不能用 _iconBtn
    
    // 2.設定姓名
    UILabel *nameLabel = (UILabel *)[view viewWithTag:2];
    view.nameLabel.text = name;

#else   // 拖線串連屬性和xib
    // 1.設定表徵圖
    //  UIButton *iconBtn = (UIButton *)[view viewWithTag:1];
    [view.iconBtn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    
    // 2.設定姓名
    //  UILabel *nameLabel = (UILabel *)[view viewWithTag:2];
    view.nameLabel.text = name;
#endif


    return view;
}
@end

 ----------------------------------------

#import <UIKit/UIKit.h>
@interface RowView : UIView
+ (id)rowViewWithIcon:(NSString *)icon name:(NSString *)name;
@property (nonatomic, weak) IBOutlet UIButton *iconBtn;      // 不是拖線的話就不要了
@property (nonatomic, weak) IBOutlet UILabel *nameLabel;   // 不是拖線的話就不要了
@end

 

 ----------------------------------------

RowView.xib:

                      

 

控制器中的用法:
- (void)viewDidLoad
{
      [super viewDidLoad];
  
      RowView *row = [RowView rowViewWithIcon:@"017.png" name:@“jake"];
      [self.view addSubview:row];
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.