您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 視圖建立:用 UIView 分類寫一套控件工廠方法

編輯:iOS 開發綜合

每當建立一個新畫面,總要重複寫一次「alloc、設定 frame、設定對齊、設定顏色」。把這些樣板收進 UIView 的分類(Category),就能用一行程式碼取得預設外觀的控件。

標頭檔

// 提供 UI 控件的工廠方法,統一處理通用初始化工作
#import <UIKit/UIKit.h>

@interface UIView (UIFactory)

// Label
+ (id)createLabel;
+ (id)createLabel:(CGRect)frame;

// TextField
+ (id)createTextFiled;
+ (id)createTextFiled:(UITextBorderStyle)style;
+ (id)createTextFiled:(CGRect)frame style:(UITextBorderStyle)style;

// Button
+ (id)createButton:(CGRect)frame;
+ (id)createButton:(CGRect)frame type:(UIButtonType)type;
+ (id)createButton:(CGRect)frame target:(id)target action:(SEL)action;
+ (id)createButton:(CGRect)frame target:(id)target action:(SEL)action
        buttonType:(UIButtonType)type;

// TableView
+ (id)createTableView:(id<UITableViewDataSource>)dataSource
            delegete:(id<UITableViewDelegate>)delegate;
+ (id)createTableView:(id<UITableViewDataSource>)dataSource
            delegete:(id<UITableViewDelegate>)delegate
               style:(UITableViewStyle)style;
+ (id)createTableView:(CGRect)frame
           dataSource:(id<UITableViewDataSource>)dataSource
            delegete:(id<UITableViewDelegate>)delegate;
+ (id)createTableView:(CGRect)frame
           dataSource:(id<UITableViewDataSource>)dataSource
            delegete:(id<UITableViewDelegate>)delegate
               style:(UITableViewStyle)style;

// TextView
+ (id)createTextView:(CGRect)frame;

@end

實作檔:先處理跨版本的巨集對照

iOS 6 是一條分水嶺,UITextAlignment 系列改名為 NSTextAlignment,UILineBreakMode 系列改名為 NSLineBreakMode。用巨集隔開,可讓同一份程式碼同時支援舊版:

#import "UIView+UIFactory.h"

#ifndef Demo_Macros_h
#define Demo_Macros_h

#ifdef __IPHONE_6_0
    #define kTextAlignmentLeft NSTextAlignmentLeft
    #define kTextAlignmentCenter NSTextAlignmentCenter
    #define kTextAlignmentRight NSTextAlignmentRight
    #define kLineBreakModeCharaterWrap NSLineBreakByCharWrapping
    #define kLineBreakModeWordWrap NSLineBreakByWordWrapping
    #define kLineBreakModeClip NSLineBreakByClipping
    #define kLineBreakModeTruncatingHead NSLineBreakByTruncatingHead
    #define kLineBreakModeTruncatingMiddle NSLineBreakByTruncatingMiddle
    #define kLineBreakModeTruncatingTail NSLineBreakByTruncatingTail
#else
    #define kTextAlignmentLeft UITextAlignmentLeft
    #define kTextAlignmentCenter UITextAlignmentCenter
    #define kTextAlignmentRight UITextAlignmentRight
    #define kLineBreakModeCharaterWrap UILineBreakModeCharacterWrap
    #define kLineBreakModeWordWrap UILineBreakModeWordWrap
    #define kLineBreakModeClip UILineBreakModeClip
    #define kLineBreakModeTruncatingHead UILineBreakModeHeadTruncation
    #define kLineBreakModeTruncatingMiddle UILineBreakModeMiddleTruncation
    #define kLineBreakModeTruncatingTail UILineBreakModeTailTruncation
#endif

#define kMainScreenFrame [[UIScreen mainScreen] bounds]
#define kMainScreenWidth kMainScreenFrame.size.width
#define kMainScreenHeight kMainScreenFrame.size.height - 20
#define kApplicationFrame [[UIScreen mainScreen] applicationFrame]

#endif

另一個細節是 __has_feature(objc_arc):在 ARC 與非 ARC 專案間切換時,release/autorelease 的處理必須分岔,否則一邊會洩漏、一邊會誤放。

@implementation UIView (UIFactory)

#pragma mark - Label

+ (id)createLabel {
    return [UIView createLabel:CGRectZero];
}

+ (id)createLabel:(CGRect)frame {
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = kTextAlignmentCenter;
#if __has_feature(objc_arc)
    return label;
#else
    return [label autorelease];
#endif
}

#pragma mark - TextField

+ (id)createTextFiled {
    return [UIView createTextFiled:UITextBorderStyleRoundedRect];
}

+ (id)createTextFiled:(UITextBorderStyle)style {
    return [UIView createTextFiled:CGRectZero style:style];
}

+ (id)createTextFiled:(CGRect)frame style:(UITextBorderStyle)style {
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
    textField.textAlignment = kTextAlignmentCenter;
    textField.textColor = [UIColor blackColor];
    textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    textField.borderStyle = style;
#if __has_feature(objc_arc)
    return textField;
#else
    return [textField autorelease];
#endif
}

#pragma mark - Button

+ (id)createButton:(CGRect)frame {
    return [UIView createButton:frame type:UIButtonTypeRoundedRect];
}

+ (id)createButton:(CGRect)frame type:(UIButtonType)type {
    UIButton *btn = [UIButton buttonWithType:type];
    btn.frame = frame;
    return btn;
}

+ (id)createButton:(CGRect)frame target:(id)target action:(SEL)action {
    return [UIView createButton:frame target:target action:action
                     buttonType:UIButtonTypeRoundedRect];
}

+ (id)createButton:(CGRect)frame target:(id)target action:(SEL)action
        buttonType:(UIButtonType)type {
    UIButton *btn = [UIButton buttonWithType:type];
    btn.frame = frame;
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

#pragma mark - TableView

+ (id)createTableView:(id<UITableViewDataSource>)dataSource
            delegete:(id<UITableViewDelegate>)delegate {
    return [UIView createTableView:CGRectZero dataSource:dataSource
                          delegete:delegate style:UITableViewStyleGrouped];
}

+ (id)createTableView:(id<UITableViewDataSource>)dataSource
            delegete:(id<UITableViewDelegate>)delegate
               style:(UITableViewStyle)style {
    return [UIView createTableView:CGRectZero dataSource:dataSource
                          delegete:delegate style:style];
}

+ (id)createTableView:(CGRect)frame
           dataSource:(id<UITableViewDataSource>)dataSource
            delegete:(id<UITableViewDelegate>)delegate {
    return [UIView createTableView:frame dataSource:dataSource
                          delegete:delegate style:UITableViewStyleGrouped];
}

+ (id)createTableView:(CGRect)frame
           dataSource:(id<UITableViewDataSource>)dataSource
            delegete:(id<UITableViewDelegate>)delegate
               style:(UITableViewStyle)style {
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:style];
    tableView.delegate = delegate;
    tableView.dataSource = dataSource;
#if __has_feature(objc_arc)
    return tableView;
#else
    return [tableView autorelease];
#endif
}

#pragma mark - TextView

+ (id)createTextView:(CGRect)frame {
    UITextView *tv = [[UITextView alloc] initWithFrame:frame];
#if __has_feature(objc_arc)
    return tv;
#else
    return [tv autorelease];
#endif
}

@end

這套寫法有兩個優點:外觀設定集中在一處,改版時不必逐頁修改;每個 create 方法都有多個 overload,呼叫端可依需要選擇只傳 frame、只傳樣式,或全部傳入。缺點則是分類無法加入實例狀態,若日後需要更複雜的預設值,就該考慮改用獨立的 Factory 類別或 XIB 繼承。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved