更輕量的 View Controllers,viewcontrollers

來源:互聯網
上載者:User

更輕量的 View Controllers,viewcontrollers

iew controllers 通常是 iOS 項目中最大的檔案,並且它們包含了許多不必要的代碼。所以 View controllers 中的代碼幾乎總是複用率最低的。我們將會看到給 view controllers 瘦身的技術,讓代碼變得可以複用,以及把代碼移動到更合適的地方。

你可以在 Github 上擷取關於這個問題的樣本項目。

把 Data Source 和其他 Protocols 分離出來

UITableViewDataSource 的代碼提取出來放到一個單獨的類中,是為 view controller 瘦身的強大技術之一。當你多做幾次,你就能總結出一些模式,並且建立出可複用的類。

舉個例,在樣本項目中,有個 PhotosViewController 類,它有以下幾個方法:

# pragma mark Pragma- (Photo*)photoAtIndexPath:(NSIndexPath*)indexPath {    return photos[(NSUInteger)indexPath.row];}- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {    return photos.count;}- (UITableViewCell*)tableView:(UITableView*)tableView        cellForRowAtIndexPath:(NSIndexPath*)indexPath {    PhotoCell* cell = [tableView dequeueReusableCellWithIdentifier:PhotoCellIdentifier                                                      forIndexPath:indexPath];    Photo* photo = [self photoAtIndexPath:indexPath];    cell.label.text = photo.name;    return cell;}

這些代碼基本都是圍繞數組做一些事情,更針對地說,是圍繞 view controller 所管理的 photos 數組做一些事情。我們可以嘗試把數組相關的代碼移到單獨的類中。我們使用一個 block 來設定 cell,也可以用 delegate 來做這件事,這取決於你的習慣。

@implementation ArrayDataSource- (id)itemAtIndexPath:(NSIndexPath*)indexPath {    return items[(NSUInteger)indexPath.row];}- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {    return items.count;}- (UITableViewCell*)tableView:(UITableView*)tableView        cellForRowAtIndexPath:(NSIndexPath*)indexPath {    id cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier                                              forIndexPath:indexPath];    id item = [self itemAtIndexPath:indexPath];    configureCellBlock(cell,item);    return cell;}@end

現在,你可以把 view controller 中的這 3 個方法去掉了,取而代之,你可以建立一個 ArrayDataSource 類的執行個體作為 table view 的 data source。

void (^configureCell)(PhotoCell*, Photo*) = ^(PhotoCell* cell, Photo* photo) {   cell.label.text = photo.name;};photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos                                                cellIdentifier:PhotoCellIdentifier                                            configureCellBlock:configureCell];self.tableView.dataSource = photosArrayDataSource;

現在你不用擔心把一個 index path 映射到數組中的位置了,每次你想把這個數組顯示到一個 table view 中時,你都可以複用這些代碼。你也可以實現一些額外的方法,比如 tableView:commitEditingStyle:forRowAtIndexPath:,在 table view controllers 之間共用。

這樣的好處在於,你可以單獨測試這個類,再也不用寫第二遍。該原則同樣適用於數組之外的其他對象。

在今年我們做的一個應用裡面,我們大量使用了 Core Data。我們建立了相似的類,但和之前使用的數組不一樣,它用一個 fetched results controller 來擷取資料。它實現了所有動畫更新、處理 section headers、刪除操作等邏輯。你可以建立這個類的執行個體,然後賦予一個 fetch request 和用來設定 cell 的 block,剩下的它都會處理,不用你操心了。

此外,這種方法也可以擴充到其他 protocols 上面。最明顯的一個就是 UICollectionViewDataSource。這給了你極大的靈活性;如果,在開發的某個時候,你想用 UICollectionView 代替 UITableView,你幾乎不需要對 view controller 作任何修改。你甚至可以讓你的 data source 同時支援這兩個協議。

將商務邏輯移到 Model 中

下面是 view controller(來自其他項目)中的範例程式碼,用來尋找一個使用者的目前的優先事項的列表:

- (void)loadPriorities {    NSDate* now = [NSDate date];    NSString* formatString = @"startDate = %@";    NSPredicate* predicate = [NSPredicate predicateWithFormat:formatString, now, now];    NSSet* priorities = [self.user.priorities filteredSetUsingPredicate:predicate];    self.priorities = [priorities allObjects];}

把這些代碼移動到 User 類的 category 中會變得更加清晰,處理之後,在 View Controller.m 中看起來就是這樣:

- (void)loadPriorities {    self.priorities = [user currentPriorities];}

User+Extensions.m 中:

- (NSArray*)currentPriorities {    NSDate* now = [NSDate date];    NSString* formatString = @"startDate = %@";    NSPredicate* predicate = [NSPredicate predicateWithFormat:formatString, now, now];    return [[self.priorities filteredSetUsingPredicate:predicate] allObjects];}

有些代碼不能被輕鬆地移動到 model 對象中,但明顯和 model 代碼緊密聯絡,對於這種情況,我們可以使用一個 Store

建立 Store 類

在我們第一版的樣本程式的中,有些代碼去負載檔案並解析它。下面就是 view controller 中的代碼:

- (void)readArchive {    NSBundle* bundle = [NSBundle bundleForClass:[self class]];    NSURL *archiveURL = [bundle URLForResource:@"photodata"                                 withExtension:@"bin"];    NSAssert(archiveURL != nil, @"Unable to find archive in bundle.");    NSData *data = [NSData dataWithContentsOfURL:archiveURL                                         options:0                                           error:NULL];    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];    _users = [unarchiver decodeObjectOfClass:[NSArray class] forKey:@"users"];    _photos = [unarchiver decodeObjectOfClass:[NSArray class] forKey:@"photos"];    [unarchiver finishDecoding];}

但是 view controller 沒必要知道這些,所以我們建立了一個 Store 對象來做這些事。通過分離,我們就可以複用這些代碼,單獨測試他們,並且讓 view controller 保持小巧。Store 對象會關心資料載入、緩衝和設定資料棧。它也經常被稱為服務層或者倉庫

把網路請求邏輯移到 Model 層

和上面的主題相似:不要在 view controller 中做網路請求的邏輯。取而代之,你應該將它們封裝到另一個類中。這樣,你的 view controller 就可以在之後通過使用回調(比如一個 completion 的 block)來請求網路了。這樣的好處是,緩衝和錯誤控制也可以在這個類裡面完成。

把 View 代碼移到 View 層

不應該在 view controller 中構建複雜的 view 階層。你可以使用 Interface Builder 或者把 views 封裝到一個 UIView 子類當中。例如,如果你要建立一個選擇日期的控制項,把它放到一個名為 DatePickerView 的類中會比把所有的事情都在 view controller 中做好好得多。再一次,這樣增加了可複用性並保持了簡單。

如果你喜歡 Interface Builder,你也可以在 Interface Builder 中做。有些人認為 IB 只能和 view controllers 一起使用,但事實上你也可以載入單獨的 nib 檔到自訂的 view 中。在樣本程式中,我們建立了一個 PhotoCell.xib,包含了 photo cell 的布局:

就像你看到的那樣,我們在 view(我們沒有在這個 nib 上使用 File's Owner 對象)上面建立了 properties,然後串連到指定的 subviews。這種技術同樣適用於其他自訂的 views。

通訊

其他在 view controllers 中經常發生的事是與其他 view controllers,model,和 views 之間進行通訊。這當然是 controller 應該做的,但我們還是希望以儘可能少的代碼來完成它。

關於 view controllers 和 model 對象之間的訊息傳遞,已經有很多闡述得很好的技術(比如 KVO 和 fetched results controllers)。但是 view controllers 之間的訊息傳遞稍微就不是那麼清晰了。

當一個 view controller 想把某個狀態傳遞給多個其他 view controllers 時,就會出現這樣的問題。較好的做法是把狀態放到一個單獨的對象裡,然後把這個對象傳遞給其它 view controllers,它們觀察和修改這個狀態。這樣的好處是訊息傳遞都在一個地方(被觀察的對象)進行,而且我們也不用糾結嵌套的 delegate 回調。這其實是一個複雜的主題,我們可能在未來用一個完整的話題來討論這個主題。

總結

我們已經看到一些用來建立更小巧的 view controllers 的技術。我們並不是想把這些技術應用到每一個可能的角落,只是我們有一個目標:寫可維護的代碼。知道這些模式後,我們就更有可能把那些笨重的 view controllers 變得更整潔。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.