標籤:c style class blog code java
Subclassing 繼承/子類大多語言允許開發人員子類化架構所提供的類,但是在 Objective-C 中不完全是這樣。大部分常用的類,諸如 NSArray、NSSet、NSDictionary 基本上都是集合類型的。不建議繼承這些類,除非你準備轉寄調用或者實現所有必要的原始方法。 在傳統的開發語言中,通常會通過繼承基礎類型(類似 NSArray 的類)來新增方法,重載已有的方法,或是自訂 UI 組件的外觀。在 Objective-C 中,一般通過 Category 來擴充新方法。通過混合方法(swizzling the method?)來重載 SDK 提供的實現。以及外觀相關的代理協議(Protocol)來定製 UI 組件的外觀。 雖說如此,還是有一些類是經常會繼承它們的,比如 UIViewController、UITableViewController、UIControl 等。繼承 UIViewController 大概是開發過程中最棒的一件事,因為它使得添加常見的功能變得異常簡單。在我開發的每個 App 中,會有一個繼承自 UIViewController 的子類,它實現了一組常用的方法。所有其他的 View Controllers 則都繼承自這個基礎類。 (譯者註:Web 開發中也常會有一個用於被繼承的 BaseController 來提供公用方法,看來開發是觸類旁通的,要多思考) 所以,以下繼承方法:
- @interface MyAppFeaturedYouTubeVideosViewController : UIViewController
應該替換成:
- @interface MyAppFeaturedYouTubeVideosFeaturedViewController : MyAppViewController
- @interface MyAppViewController : UIViewController
這個公用基礎類可以在後續開發過程中用來添加公用的方法。在這個基礎父類中,我通常會申明以下方法:
- -(UIView*) errorView;
- -(UIView*) loadingView;
- -(void) showLoadingAnimated:(BOOL) animated;
- -(void) hideLoadingViewAnimated:(BOOL) animated;
- -(void) showErrorViewAnimated:(BOOL) animated;
- -(void) hideErrorViewAnimated:(BOOL) animated;
實現如下:
-(UIView*) errorView { return nil; } -(UIView*) loadingView { return nil; } -(void) showLoadingAnimated:(BOOL) animated { UIView *loadingView = [self loadingView]; loadingView.alpha = 0.0f; [self.view addSubview:loadingView]; [self.view bringSubviewToFront:loadingView]; double duration = animated ? 0.4f:0.0f; [UIView animateWithDuration:duration animations:^{ loadingView.alpha = 1.0f; }]; } -(void) hideLoadingViewAnimated:(BOOL) animated { UIView *loadingView = [self loadingView]; double duration = animated ? 0.4f:0.0f; [UIView animateWithDuration:duration animations:^{ loadingView.alpha = 0.0f; } completion:^(BOOL finished) { [loadingView removeFromSuperview]; }]; } -(void) showErrorViewAnimated:(BOOL) animated { UIView *errorView = [self errorView]; errorView.alpha = 0.0f; [self.view addSubview:errorView]; [self.view bringSubviewToFront:errorView]; double duration = animated ? 0.4f:0.0f; [UIView animateWithDuration:duration animations:^{ errorView.alpha = 1.0f; }]; } -(void) hideErrorViewAnimated:(BOOL) animated { UIView *errorView = [self errorView]; double duration = animated ? 0.4f:0.0f; [UIView animateWithDuration:duration animations:^{ errorView.alpha = 0.0f; } completion:^(BOOL finished) { [errorView removeFromSuperview]; }]; }
現在,App 中的每個 View Controller 中,可以很方便的通過調用以上方法來改變當前 View 的狀態為 Loading 或者 Error。而且,View Controller 可以通過重載 -errorView 和 -loadingView 方法來提供自訂錯誤介面和 Loading 介面。 你還可以通過重載這個基礎類中的 -viewDidLoad 來統一修改所有 View 的表現。比如為所有的 View 添加相同的背景圖片或背景色:
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor appOffWhiteColor]; // changes all my views to "off-white" }
UI Customization 自訂 UI自訂 UI 可以大致分成兩類,一是自訂控制項,二是皮膚/主題。前者可以讓 App 更出色,而後者是大部分 App 都需要的。我建議給 UIFont 和 UIColor 寫 Category 擴充來提供自訂字型和自訂色彩。 例如,給 UIFont 添加如下方法:
-
+(UIFont*) appFontOfSize:(CGFloat) pointSize { return [UIFont fontWithName:@"MyriadPro-Regular" size:pointSize]; } +(UIFont*) boldAppFontOfSize:(CGFloat) pointSize { return [UIFont fontWithName:@"MyriadPro-Black" size:pointSize]; }
你就可以很方便地使用 [UIFont appFontOfSize:13] 得到 MyriadPro-Regular 字型。這樣當你的設計需求變更時,就可以很快速的更換整個 App 中的字型。 相同的設計模式也可以應用到自訂色彩中。給 UIColor 添加以下方法:
-
#define GREY(color) [UIColor colorWithRed:color/255.0 green:color/255.0 blue:color/255.0 alpha:1] +(UIColor*) appBackgroundColor { return [UIColor colorWithPatternImage:[UIImage imageNamed:@"BGPattern"]]; } +(UIColor*) appBlack1Color { return GREY(38); } +(UIColor*) appOffWhiteColor { return GREY(234); }
所以,千萬不要用 Interface Builder 來選顏色。Subclassing UILabels 繼承 UILabel還有一個小竅門,當開發人員繼承 UILabel、UITextField 和 UITextView 時,通常在 -initWithFrame: 和 -initWithCoder: 方法中設定字型和顏色,參見以下代碼:
-
@implementation AppPrefixLabel -(void) setup { self.font = [UIFont fontWithName:@"SourceSansPro-Semibold" size:self.font.pointSize]; self.textColor = [UIColor redColor]; } -(id) initWithFrame:(CGRect)frame { if((self = [super initWithFrame:frame])) { [self setup]; } return self; } -(id) initWithCoder:(NSCoder *)aDecoder { if((self = [super initWithCoder:aDecoder])) { [self setup]; } return self; } @end
這個技巧使得開發人員可以在 Interface Builder 中自訂這些元素的外觀。在 IB 中拖入一個 UILabel,並且修改它的類為你自訂的類,瞬間就完成了這個 Label 字型和顏色的自訂,不用任何多餘的代碼。這個技巧多數情況下相當管用,但是當你的 App 支援自訂佈景主題,且使用者可以通過設定介面更換主題時,就會顯得有些麻煩。 -initWithFrame: 和 initWithCoder: 會在 UI 組件建立的時候被調用,所以在這之後如果要改變字型和顏色,就需要很多額外的代碼。因此,如果你的 App 支援主題,寫一個主旨管理員器的全域單例來提供全域的主題、字型、顏色。 如果你用到了我說的第一個方法,你的 UIFont 的 Category 現在可以這樣實現了:
-
+(UIFont*) appFontOfSize:(CGFloat) pointSize { NSString *currentFontName = [[ThemeProvider sharedInstance] currentFontName]; return [UIFont fontWithName:currentFontName size:pointSize]; }
UIColor 同理。其實沒有正確或錯誤的方法,上述方法都是可行的。 遵從這裡提到的設計模式,可以讓你的代碼乾淨得像寫的很漂亮的 JS/CSS。試著在你的下一個項目中用這些方法吧。
Allen 後記之前在想 iOS 開發到底是否需要一個類似 Web 開發中的所謂的架構,但漸漸發現其實 iOS SDK 本就是一個高度封裝了的架構了,可能我們需要的不是更更高層的架構,而是一種好的設計模式、開發習慣和代碼結構。因此是不是可以從一個 Project 的層面出發,寫一個乾淨的架構,並且定義一些規範,就是一個很好的“架構”了?而不是非得提供 Router 之類的往 Web 開發架構去靠。