標籤:uisplitviewcontrolle ipad delegate webview
最終:
主控制器 BeyondViewController
繼承自UISplitViewController
//// BeyondViewController.h// 27_SplitViewCtroller//// Created by beyond on 14-8-31.// Copyright (c) 2014年 com.beyond. All rights reserved.// 主控制器,繼承自UISplitViewController,左邊master控制器是:FoodTypeListCtrl,右邊的從控制器是FoodListCtrl#import <UIKit/UIKit.h>@interface BeyondViewController : UISplitViewController@end
//// BeyondViewController.m// 27_SplitViewCtroller//// Created by beyond on 14-8-31.// Copyright (c) 2014年 com.beyond. All rights reserved.// 主控制器,繼承自UISplitViewController,左邊master控制器是:FoodTypeListCtrl,右邊的從控制器是FoodListCtrl#import "BeyondViewController.h"// 菜系 列表 控制器#import "FoodTypeListController.h"// 菜列表 控制器#import "FoodListController.h"// 菜系 列表 控制器 定義的協議,目的是:點擊了菜系 列表 中的某一行時,告訴代理(即菜列表控制器) 要展示哪一種菜系下面的所有菜#import "FoodTypeListCtrlDelegate.h"@interface BeyondViewController () <FoodTypeListCtrlDelegate>@end@implementation BeyondViewController// 重要~~~主控制器充當中間人,先得到FoodTypeListController,並且成為它的代理,得到菜系 列表 的某一行被點擊時 對應的菜系對象// 然後再在FoodTypeListController的代理方法中,得到FoodListController,並將菜系 進一步傳遞給FoodListController- (void)viewDidLoad{ [super viewDidLoad]; // 1.先得到Master控制器,即導航控制器,再從導航控制器中得到 FoodTypeListController,並且成為它的代理 UINavigationController *foodTypeListNav = [self.childViewControllers firstObject]; FoodTypeListController *foodTypeListCtrl = [foodTypeListNav.childViewControllers firstObject]; foodTypeListCtrl.delegate = self; // 讓foodListCtrl成為主控制器的代理,僅僅是監聽Master控制器的出現和隱藏,並在foodListCtrl的左按鈕上顯示相應的提示文字(點擊該文字可以展開被隱藏的Master控制器) UINavigationController *foodListNav = [self.childViewControllers lastObject]; FoodListController<UISplitViewControllerDelegate> *foodListCtrl = [foodListNav.childViewControllers firstObject]; self.delegate = foodListCtrl;}// 2. 然後再在FoodTypeListController的代理方法中,得到FoodListController,並將菜系 進一步傳遞給FoodListController- (void)foodTypeListController:(FoodTypeListController *)foodTypesVc didSelectedFoodType:(FoodType *)type{ UINavigationController *foodListNavi = [self.childViewControllers lastObject]; FoodListController *foodListCtrl = [foodListNavi.childViewControllers firstObject]; foodListCtrl.foodType = type; [foodListNavi popToRootViewControllerAnimated:YES];}@end
SplitViewCtrl的Master主控制器,
繼承自表格控制器
FoodTypeListController 【菜系列表】及其代理
//// FoodTypeListController.h// 27_SplitViewCtroller//// Created by beyond on 14-8-31.// Copyright (c) 2014年 com.beyond. All rights reserved.// SplitViewCtrl的Master主控制器,繼承自表格控制器 【菜系列表】#import <UIKit/UIKit.h>@protocol FoodTypeListCtrlDelegate;@interface FoodTypeListController : UITableViewController// 成員:代理,當點擊了某一行時,告訴代理 即點擊了哪一種菜系,從而,右邊的控制器展示相應的菜名列表@property (weak, nonatomic) id<FoodTypeListCtrlDelegate> delegate;@end
//// FoodTypeListController.m// 27_SplitViewCtroller//// Created by beyond on 14-8-31.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "FoodTypeListController.h"#import "FoodType.h"#import "FoodTypeListCtrlDelegate.h"@interface FoodTypeListController ()// 成員:數組,儲存著從plist中載入的所有的從字典一一轉成對象的FoodType@property (strong, nonatomic) NSArray *foodTypesArr;@end@implementation FoodTypeListController// getter訪問時才載入,懶載入- (NSArray *)foodTypesArr{ if (_foodTypesArr == nil) { // 經典,一句話將參數所對應的Plist檔案中的字典數組,轉成 該類的對象數組 _foodTypesArr = [FoodType objArrFromPlistName:@"food_types.plist"]; } return _foodTypesArr;}- (void)viewDidLoad{ [super viewDidLoad]; self.title = @"菜系"; // 預設選擇第0行,調用自己的方法,給其傳遞資料模型 [self.tableView selectRowAtIndexPath:kIndexPathZero animated:YES scrollPosition:UITableViewScrollPositionTop]; // 讓第0行,顯示選中狀態,需配合覆蓋掉系統預設的方法:viewWillAppear [self tableView:self.tableView didSelectRowAtIndexPath:kIndexPathZero];}// 取消系統預設的一些 事件,讓第0行,顯示選中狀態- (void)viewWillAppear:(BOOL)animated{ // do nothing...}#pragma mark - 資料來源方法// 多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.foodTypesArr.count;}// 每行顯示的cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"FoodType"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } // 模型數組中取出對應行的模型 FoodType *type = self.foodTypesArr[indexPath.row]; // 設定獨一無二的內容 cell.textLabel.text = type.name; // 返回cell return cell;}#pragma mark - 代理方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // 如果 代理(就是右邊的從控制器)需要,才告知 當前選擇了哪一個菜系 if ([self.delegate respondsToSelector:@selector(foodTypeListController:didSelectedFoodType:)]) { FoodType *type = self.foodTypesArr[indexPath.row]; [self.delegate foodTypeListController:self didSelectedFoodType:type]; }}@end
定義好的協議
//// FoodTypeListCtrlDelegate.h// 27_SplitViewCtroller//// Created by beyond on 14-8-31.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import <Foundation/Foundation.h>@class FoodType;@protocol FoodTypeListCtrlDelegate <NSObject>@optional// 當點擊了某一行時,告訴代理 即點擊了哪一種菜系,從而,右邊的控制器展示相應的菜名列表- (void)foodTypeListController:(FoodTypeListController *)ctrl didSelectedFoodType:(FoodType *)foodType;@end
FoodListController
SplitViewCtrl的從控制器,繼承自表格控制器
展示的是某一菜系下所有的菜【菜的列表】
//// FoodListController.h// 27_SplitViewCtroller//// Created by beyond on 14-8-31.// Copyright (c) 2014年 com.beyond. All rights reserved.// SplitViewCtrl的從控制器,繼承自表格控制器 某一菜系下所有的【菜的列表】// 點擊左邊菜系列表控制器中的某一行時,本控制器將展示該菜系下的所有菜#import <UIKit/UIKit.h>@class FoodType;@interface FoodListController : UITableViewController// 資料來源,根據傳入的菜系,通過它的idstr,拼湊出新的plist名稱,載入,轉成Food模型對象數組@property (strong, nonatomic) FoodType *foodType;@end
//// FoodListController.m// 27_SplitViewCtroller//// Created by beyond on 14-8-31.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "FoodListController.h"#import "FoodDetailController.h"#import "Food.h"#import "FoodCell.h"#import "FoodType.h"// 成為UISplitViewControllerDelegate的代理,目的是監聽其顯示和隱藏方法,從而控制leftBarButtonItem的顯示和隱藏@interface FoodListController ()<UISplitViewControllerDelegate>@property (strong, nonatomic) NSArray *foodsArr;@end@implementation FoodListController- (void)viewDidLoad{ [super viewDidLoad]; }// 重要~~~攔截setterFoodType方法,設定標題,從對應的Plist載入資料,並轉成對象數組,重新整理表格- (void)setFoodType:(FoodType *)foodType{ _foodType = foodType; NSString *filename = [NSString stringWithFormat:@"type_%@_foods.plist", foodType.idstr]; // 經典,一句話將參數所對應的Plist檔案中的字典數組,轉成 該類的對象數組 self.foodsArr = [Food objArrFromPlistName:filename]; self.title = foodType.name; if (self.isViewLoaded) { // 預設讓tableView 滾動到第 0 行 [self.tableView scrollToRowAtIndexPath:kIndexPathZero atScrollPosition:UITableViewScrollPositionTop animated:YES]; // 重新整理表格 [self.tableView reloadData]; }}#pragma mark - SplitViewCtrl的代理方法// 即將顯示 Master主控制器- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{ self.navigationItem.leftBarButtonItem = nil;}// 即將隱藏 Master主控制器- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc{ barButtonItem.title = @"菜系"; self.navigationItem.leftBarButtonItem = barButtonItem;}#pragma mark - 資料來源方法// 多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.foodsArr.count;}// 高度封裝了FoodCell,控制器知道得很少- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ FoodCell *cell = [FoodCell cellWithTableView:tableView]; cell.food = self.foodsArr[indexPath.row]; return cell;}#pragma mark - 代理方法// cell 行高- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 100;}// 選中某一行- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // 點擊那行 對應的模型 Food *food = self.foodsArr[indexPath.row]; FoodDetailController *detailVc = [[FoodDetailController alloc] init]; // 傳遞資料模型,內部會攔截 detailVc.food = food; [self.navigationController pushViewController:detailVc animated:YES];}@end
WebView展示某一道 菜 的詳情
//// FoodDetailController.h// 27_SplitViewCtroller//// Created by beyond on 14-8-31.// Copyright (c) 2014年 com.beyond. All rights reserved.// 點擊菜列表控制中的某一行,來到這,顯示該道菜的詳細資料,直接用webView展示#import <UIKit/UIKit.h>@class Food;@interface FoodDetailController : UIViewController// 資料來源,要顯示哪道菜@property (strong, nonatomic) Food *food;@end
//// FoodDetailController.m// 27_SplitViewCtroller//// Created by beyond on 14-8-31.// Copyright (c) 2014年 com.beyond. All rights reserved.// 點擊一行,來到這,顯示該道菜的詳細資料,直接用webView展示#import "FoodDetailController.h"#import "Food.h"@interface FoodDetailController ()@property (weak, nonatomic) UIWebView *webView;@end@implementation FoodDetailController// 讓weibView就是控制器的view- (void)loadView{ UIWebView *webView = [[UIWebView alloc] init]; // bounds就是螢幕的全部地區,applicationFrame就是app顯示的地區,不包含狀態列 webView.frame = [UIScreen mainScreen].applicationFrame; // 隨著螢幕的旋轉,寬高自動調整 webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.view = webView; // 成員變數,記住,目的是 避免強轉 self.webView = webView;}- (void)viewDidLoad{ [super viewDidLoad]; // 導覽列標題 self.title = self.food.name; // 重點~~~拼接本地url,注意:如果在沙箱建立真實的檔案夾,那麼負載檔案時,要加上檔案夾名 NSString *fileName = [NSString stringWithFormat:@"Html/food/%@.html", self.food.idstr]; NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:fileName withExtension:nil]]; [self.webView loadRequest:request];}@end
模型Model
//// Food.h// 27_SplitViewCtroller//// Created by beyond on 14-8-31.// Copyright (c) 2014年 com.beyond. All rights reserved.// 模型:一種菜肴 Food 對應plist檔案裡面的一個字典#import <Foundation/Foundation.h>@interface Food : NSObject// 必須與plist檔案中 字典的key 一模一樣// id@property (copy, nonatomic) NSString *idstr;// 菜名@property (copy, nonatomic) NSString *name;// 小表徵圖url@property (copy, nonatomic) NSString *imageUrl;// 網頁的url@property (copy, nonatomic) NSString *url;// 本道菜預計耗時多長@property (copy, nonatomic) NSString *time;// 製作難度多大@property (copy, nonatomic) NSString *diff;@end
//// FoodType.h// 27_SplitViewCtroller//// Created by beyond on 14-8-31.// Copyright (c) 2014年 com.beyond. All rights reserved.// 模型:一種菜系 FoodType 對應plist檔案裡面的一個字典#import <Foundation/Foundation.h>@interface FoodType : NSObject// ID@property (copy, nonatomic) NSString *idstr;// 菜系 名稱:如粵菜 川菜 家常菜@property (copy, nonatomic) NSString *name;@end
封裝的一個Cell View
//// FoodCell.h// 27_SplitViewCtroller//// Created by beyond on 14-8-31.// Copyright (c) 2014年 com.beyond. All rights reserved.// 自訂cell,一個cell 展示一個food模型裡面的資料,傳入tableView執行個體化cell的目的是封裝得最徹底,讓控制器作最少的事,知道得最少#import <UIKit/UIKit.h>@class Food;@interface FoodCell : UITableViewCell// 資料來源模型,提供資料給內部的子控制項們顯示,內部會攔截setter方法@property (strong, nonatomic) Food *food;// 傳入tableView執行個體化cell的目的是封裝得最徹底,讓控制器作最少的事,知道得最少+ (instancetype)cellWithTableView:(UITableView *)tableView;@end
//// FoodCell.m// 27_SplitViewCtroller//// Created by beyond on 14-8-31.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "FoodCell.h"#import "Food.h"#import "UIImageView+WebCache.h"@interface FoodCell()@property (weak, nonatomic) IBOutlet UIImageView *iconView;@property (weak, nonatomic) IBOutlet UILabel *nameLabel;@property (weak, nonatomic) IBOutlet UILabel *descLabel;@end@implementation FoodCell// 傳入tableView執行個體化cell的目的是封裝得最徹底,讓控制器作最少的事,知道得最少+ (instancetype)cellWithTableView:(UITableView *)tableView{ // cellID必須和xib中的一模一樣 static NSString *cellID = @"FoodCell"; FoodCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (cell == nil) { // 從xib建立 cell = [[[NSBundle mainBundle] loadNibNamed:cellID owner:nil options:nil] lastObject]; // 右邊是箭頭 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } return cell;}// 資料來源模型,提供資料給內部的子控制項們顯示,內部會攔截setter方法- (void)setFood:(Food *)food{ _food = food; // 小表徵圖 [self.iconView setImageWithURL:[NSURL URLWithString:food.imageUrl] placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]]; // 菜名 self.nameLabel.text = food.name; // 子標題 self.descLabel.text = [NSString stringWithFormat:@"難度:%@ 時間長度:%@", food.diff, food.time]; }@end
XIB
iOS_27SplitViewController的簡單使用