使用步驟
要想顯示一個UIPopoverController,需要經過下列步驟
設置內容控制器
由于UIPopoverController直接繼承自NSObject,不具備可視化的能力
因此UIPopoverController上面的內容必須由另外一個繼承自UIViewController的控制器來提供,這個控制器稱為“內容控制器”
設置內容的尺寸
顯示出來占據多少屏幕空間
設置顯示的位置
從哪個地方冒出來
設置內容控制器
設置內容控制器有3種方法
在初始化UIPopoverController的時候傳入一個內容控制器
- (id)initWithContentViewController:(UIViewController *) viewController;
@property (nonatomic, retain) UIViewController *contentViewController;
- (void)setContentViewController:(UIViewController*)viewController animated:(BOOL)animated;
以上方法和屬性都是UIPopoverController的
設置內容的尺寸
設置內容的尺寸有2種方法
@property (nonatomic) CGSize popoverContentSize;
- (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated;
以上方法和屬性都是UIPopoverController的
設置顯示的位置
設置顯示的位置有2種方法
圍繞著一個UIBarButtonItem顯示(箭頭指定那個UIBarButtonItem)
- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;
item :圍繞著哪個UIBarButtonItem顯示
arrowDirections :箭頭的方向
animated :是否通過動畫顯示出來
設置顯示的位置
圍繞著某一塊特定區域顯示(箭頭指定那塊特定區域)
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView
*)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
animated:(BOOL)animated;
rect :指定箭頭所指區域的矩形框范圍(位置和尺寸)
view :rect參數是以view的左上角為坐標原點(0,0)
arrowDirections:箭頭的方向
animated :是否通過動畫顯示出來
設置顯示的位置
? 如果想讓箭頭指向某一個UIView的做法有2種做法,比如指向一個button
? 方法1
[popover presentPopoverFromRect:button.bounds inView:button permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
? 方法2
[popover presentPopoverFromRect:button.frame inView:button.superview permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
常見報錯
? 在popover的使用過程中,經常會遇到這個錯誤
-[UIPopoverController dealloc] reached while popover is still visible.
? 錯誤的大體意思是:popover在仍舊可見的時候被銷毀了(調用了dealloc)
? 從錯誤可以得出的結論
? 當popover仍舊可見的時候,不準銷毀popover對象
? 在銷毀popover對象之前,一定先讓popover消失(不可見)
通過內容控制器設置內容尺寸
? 內容控制器可以自行設置自己在popover中顯示的尺寸
? 在iOS 7之前
@property (nonatomic,readwrite) CGSize contentSizeForViewInPopover;
? 從iOS 7開始
@property (nonatomic) CGSize preferredContentSize;
以上屬性都是UIViewController的
常用屬性
? 代理對象
@property (nonatomic, assign) id <UIPopoverControllerDelegate> delegate;
? 是否可見
@property (nonatomic, readonly, getter=isPopoverVisible) BOOL popoverVisible;
? 箭頭方向
@property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection;
? 關閉popover(讓popover消失)
- (void)dismissPopoverAnimated:(BOOL)animated;
防止點擊UIPopoverController區域外消失
? 默認情況下
? 只要UIPopoverController顯示在屏幕上,UIPopoverController背后的所有控件默認是不能跟用戶進行正常交互的
? 點擊UIPopoverController區域外的控件,UIPopoverController默認會消失
? 要想點擊UIPopoverController區域外的控件時不讓UIPopoverController消失,解決辦法是設置passthroughViews屬性
@property (nonatomic, copy) NSArray *passthroughViews;
? 這個屬性是設置當UIPopoverController顯示出來時,哪些控件可以繼續跟用戶進行正常交互。這樣的話,點擊區域外的控件就不會讓UIPopoverController消失了
如何iPhone中實現popover的效果
? UIPopoverController這個類是只能用在iPad中的
? 要想在iPhone中實現popover效果,必須得自定義view,可以參考
? http://code4app.com/ios/Popover-View-in-iPhone/4fa931bd06f6e78d0f000000
? http://code4app.com/ios/Popup-Menu/512231ac6803fa9e08000000
*******************筆記*********************
UIPopoverController的簡單使用
1.iPad特有的控制器
2.繼承自NSObect,并非繼承自UIViewController
2.1繼承自NSObect:不是一個控件類的東西,就是不可見,不具備可視化能力,如果要讓它可見,就要把它方法哦某個控件中,控制器才可以顯示
2.2.繼承自UIViewController或者UIview:UIViewController里面有個View,可以通View展示東西,如果繼承UIView,可以把這個東西展示到這個UIView上面
3.特點:只占據屏幕的部分控件呈現信息,并顯示在最前面
4.使用步驟(IOS8之前)
1.設置內容控制器
MenuViewController *menuVC = [[MenuViewController alloc] init];
2.初始化UIPopoverController并指定內容控制器(初始化的時候必須要指定)
UIPopoverController *menuPopover = [[UIPopoverController alloc] initWithContentViewController:menuVC];
3.設置內容控制器的大小
menuPopover.popoverContentSize = CGSizeMake(110, 44 * 3);
4.彈出UIPopverController,設置顯示位置,并指定箭頭方向
[menuPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
- 4.退出UIPopverController
[menuPopover dismissPopoverAnimated:YES];
UIPopoverController的其他用法
- 一.導航條控制器的點擊
1.懶加載
- (UIPopoverController *)controllerPopover
{
if (_controllerPopover == nil) {
1.創建內容控制器
OneViewController *oneVC = [[OneViewController alloc] init];
UINavigationController *oneNav = [[UINavigationController alloc] initWithRootViewController:oneVC];
2.初始化UIPopoverController并設置內容控制器
self.controllerPopover = [[UIPopoverController alloc] initWithContentViewController:oneNav];
}
return _controllerPopover;
}
-二.設置導航條內容
- (void)viewDidLoad {
[super viewDidLoad];
1.設置背景顏色
self.view.backgroundColor = [UIColor whiteColor];
2.添加導航條文字
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];
3.添加下一個控制器按鈕
UIButton *nextBtn = [[UIButton alloc] init];
[nextBtn setTitle:@"到下一個控制器" forState:UIControlStateNormal];
nextBtn.backgroundColor = [UIColor redColor];
nextBtn.frame = CGRectMake(100, 100, 140, 40);
[self.view addSubview:nextBtn];
[nextBtn addTarget:self action:@selector(nextBtn) forControlEvents:UIControlEventTouchUpInside];
```
4.設置返回按鈕樣式
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
}
- (void)nextBtn
{
TwoViewController *twoVC = [[TwoViewController alloc] init];
[self.navigationController pushViewController:twoVC animated:YES];
}
-三.顏色的點擊
懶加載
- (UIPopoverController *)colorPopver {
if (_colorPopver == nil) {
1.創建內容控制器
ColorViewController *colorVC = [[ColorViewController alloc] init];
2.創建UIPopverController,并設置內容控制器
_colorPopver = [[UIPopoverController alloc] initWithContentViewController:colorVC];
3.封裝block代碼
__weak typeof(self)weakSelf = self;
colorVC.colorChooseBlock = ^(UIColor *color){
1.設置背景顏色
self.view.backgroundColor = color;
2.popver消失
[weakSelf.colorPopver dismissPopoverAnimated:YES];
};
}
return _colorPopver;
}
點擊
- (IBAction)colorClick:(UIButton *)sender {
[self.colorPopver presentPopoverFromRect:sender.bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
(void)viewDidLoad {
[super viewDidLoad];
1.設置圖片的真實大小
self.preferredContentSize = self.imageView.image.size;
}-
(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
1.獲取當前點擊的點
CGPoint point = [[touches anyObject] locationInView:self.view];2.獲取點的顏色
UIColor *color = [self.imageView.image pixelColorAtLocation:point];3.調用block
if (self.colorChooseBlock) {
self.colorChooseBlock(color);
}
}
- 四ios8點擊
懶加載
-
(Ios8ViewController *)ios8VC {
if (_ios8VC == nil) {
1.創建內容控制器
_ios8VC = [[Ios8ViewController alloc] init];2.設置彈出樣式為popover _ios8VC.modalPresentationStyle = UIModalPresentationPopover;
}
return _ios8VC;
}
彈出
-
(IBAction)ios8Click:(UIButton *)sender {
1.設置彈出位置
self.ios8VC.popoverPresentationController.sourceView = self.view;
self.ios8VC.popoverPresentationController.sourceRect = sender.frame;2.以modal的形式彈出
[self presentViewController:self.ios8VC animated:YES completion:nil];
}
美團界面搭建(一)
1.設置導航條背景圖片(通過自定義導航條)
2.使用xib布局,設置導航條按鈕
3.取消導航條圖片渲染
- 一 自定義導航條,設置背景
/**
* 類在第一次使用的時候調用
*/
-
(void)initialize {
1.拿到navigationBar
UINavigationBar *navBar = [UINavigationBar appearance];2.設置背景顏色
[navBar setBackgroundImage:[UIImage imageNamed:@"bg_navigationBar_normal"] forBarMetrics:UIBarMetricsDefault];
}
- 二 添加導航條內容
-
(void)setupBarButtonItems
{
1.添加logo
UIBarButtonItem *logoItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_meituan_logo"] style:UIBarButtonItemStyleDone target:nil action:nil];
logoItem.enabled = NO;2.添加頂部item
XMGTopView *topView = [XMGTopView topView];UIBarButtonItem *topViewItem = [[UIBarButtonItem alloc] initWithCustomView:topView];
self.navigationItem.leftBarButtonItems = @[logoItem,topViewItem];
}
美團界面搭建(類別的展示)
一.xib描述XMGTopItemView
pragma mark - 快速創建xib
-(instancetype)topItemView { return [[[NSBundle mainBundle] loadNibNamed:@"XMGTopItemView" owner:nil options:nil] firstObject]; }
pragma - mark - 修改屬性內容
-(void)setTitle:(NSString *)title { self.titleLabel.text = title; }
-(void)setSubTitle:(NSString *)subTitle { self.subTitleLabel.text = subTitle; }
-(void)setIconWithimage:(NSString )image highImage:(NSString )highImage { [self.iconButton setImage:[UIImage imageNamed:image] forState:UIControlStateNormal]; [self.iconButton setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted]; }
pragma - mark 對外提供監聽點擊
(void)addTarget:(id)target action:(SEL)action { [self.iconButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; } ```
二.XMGHomeViewController
pragma mark - 懶加載
(XMGCategoryController *)categoryVC { if (categoryVC == nil) {
1.創建內容控制器 categoryVC = [[XMGCategoryController alloc] init];
2.設置彈出樣式 _categoryVC.modalPresentationStyle = UIModalPresentationPopover;
} return _categoryVC; }
(void)categoryClick { 1.彈出位置 self.categoryVC.popoverPresentationController.barButtonItem = self.categoryItem;
2.以modal形式彈出
[self presentViewController:_categoryVC animated:YES completion:nil]; }
三.XMGLRTableView 快速創建
-(instancetype)lrTableView { return [[[NSBundle mainBundle] loadNibNamed:@"XMGLRTableView" owner:nil options:nil] firstObject]; }
pragma mark - tableview數據
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.leftTableView) { 左邊的tableView return self.categoryData.count; } else { 右邊的tableView return self.subCategoryData.count; }
}
-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell cell = nil; if (tableView == self.leftTableView) { 左邊的tableView cell = [XMGLeftCell leftCellWithTabelView:tableView]; 取出模型 XMGCategory category = self.categoryData[indexPath.row]; cell.textLabel.text = category.name; cell.imageView.image = [UIImage imageNamed:category.small_icon]; cell.imageView.highlightedImage = [UIImage imageNamed:category.small_highlighted_icon]; } else { 右邊的tableView
cell = [XMGRightCell rightCellWithTabelView:tableView];
cell.textLabel.text = self.subCategoryData[indexPath.row];
} return cell; }
pragma mark - 代理
-(void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath { if (tableView == self.leftTableView) { 左邊的tableView
1.取出模型 XMGCategory *category = self.categoryData[indexPath.row];
2.記錄數據 self.subCategoryData = category.subcategories; 3.刷新游標表格 [self.rightTableView reloadData];
} else { 右邊的tableView
} }
- 四.XMGCategoryController
-
(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];1.添加左右tableView XMGLRTableView *lrTabelView = [XMGLRTableView lrTableView]; lrTabelView.categoryData = [XMGCategory objectArrayWithFilename: @"categories.plist"]; lrTabelView.frame = self.view.bounds; 2.隨父控件的拉伸而拉伸 lrTabelView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [self.view addSubview:lrTabelView];
}
- (NSArray *)categoryData {
if (!_categoryData) {
_categoryData = [XMGCategory objectArrayWithFile:@"categories.plist"];
}
return _categoryData;
}
- (NSArray *)categoryData {
封裝LRTableView
- 一.XMGLRTableView
1.自定義協議以及代理
@class XMGLRTableView;
@protocol XMGLRTableViewDataSource <NSObject>
@required
/** 左邊返回多少行 */
- (NSInteger)numOfLeftRowsWithLRTableView:(XMGLRTableView )lrTableView;
/* 左邊第幾行返回的文字 */ - (NSString )lrTableView:(XMGLRTableView )lrTableView titleInRow:(NSInteger)row;
/ 左邊第幾行返回的右邊的子數據 */ - (NSArray )lrTableView:(XMGLRTableView )lrTableView subDataInRow:(NSInteger)row;
@optional
/ 返回普通圖片 */ - (NSString )lrTableView:(XMGLRTableView )lrTableView imageInRow:(NSInteger)row;
/ 返回選中圖片 */ - (NSString *)lrTableView:(XMGLRTableView *)lrTableView highImageInRow:(NSInteger)row;
@end
@protocol XMGLRTableViewDelegate <NSObject>
@optional
/** 點擊左邊,告知點擊的行數 */
- (void)lrTableView:(XMGLRTableView )lrTableView seletedLeftRow:(NSInteger)leftRow;
/* 點擊左邊,告知點擊的左右行數 */ - (void)lrTableView:(XMGLRTableView *)lrTableView seletedLeftRow:(NSInteger)leftRow seletedRightRow:(NSInteger)rightRow;
@end
- 一.1 調用協議與代理
pragma mark - tableview數據源
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.leftTableView) { 左邊的tableView
return [self.dataSource numOfLeftRowsWithLRTableView:self];
} else { 右邊的tableView
return self.subData.count;
}
}-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (tableView == self.leftTableView) { 左邊的tableView
cell = [XMGLeftCell leftCellWithTabelView:tableView];
左邊的文字
cell.textLabel.text = [self.dataSource lrTableView:self titleInRow:indexPath.row];
左邊的圖片,可實現
if ([self.dataSource respondsToSelector:@selector(lrTableView:imageInRow:)]) {
cell.imageView.image = [UIImage imageNamed:[self.dataSource lrTableView:self imageInRow:indexPath.row]];
}if ([self.dataSource respondsToSelector:@selector(lrTableView:highImageInRow:)]) { cell.imageView.highlightedImage =[UIImage imageNamed:[self.dataSource lrTableView:self highImageInRow:indexPath.row]]; }
} else { 右邊的tableView
cell = [XMGRightCell rightCellWithTabelView:tableView]; cell.textLabel.text = self.subData[indexPath.row];
}
return cell;
}
pragma mark - tableview代理
-
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.leftTableView) { 左邊的tableView
1.獲取子數據
self.subData = [self.dataSource lrTableView:self subDataInRow:indexPath.row];2.刷新右邊表格 [self.rightTableView reloadData]; 3.通知代理被點了 if ([self.delegate respondsToSelector:@selector(lrTableView:seletedLeftRow:)]) { [self.delegate lrTableView:self seletedLeftRow:indexPath.row]; } 4.記錄行數 self.seleteLeftRow = indexPath.row;
} else { 右邊的tableView
通知代理被點了
if ([self.delegate respondsToSelector:@selector(lrTableView:seletedLeftRow:seletedRightRow:)]) {
[self.delegate lrTableView:self seletedLeftRow:self.seleteLeftRow seletedRightRow:indexPath.row];
}}
} 二.實現協議與代理XMGCategoryController
pragma mark - 實現XMGLRTableView數據源方法(NSInteger)numOfLeftRowsWithLRTableView:(XMGLRTableView *)lrTableView {
return self.categoryData.count;
}(NSString *)lrTableView:(XMGLRTableView *)lrTableView titleInRow:(NSInteger)row {
XMGCategory *category = self.categoryData[row];
return category.name;
}(NSArray *)lrTableView:(XMGLRTableView *)lrTableView subDataInRow:(NSInteger)row {
XMGCategory *category = self.categoryData[row];
return category.subcategories;
}(NSString *)lrTableView:(XMGLRTableView *)lrTableView imageInRow:(NSInteger)row {
XMGCategory *category = self.categoryData[row];
return category.small_icon;
}(NSString *)lrTableView:(XMGLRTableView *)lrTableView highImageInRow:(NSInteger)row {
XMGCategory *category = self.categoryData[row];
return category.small_highlighted_icon;
}
pragma mark - 實現XMGLRTableView代理
(void)lrTableView:(XMGLRTableView *)lrTableView seletedLeftRow:(NSInteger)leftRow {
1.獲取數據
XMGCategory *category = self.categoryData[leftRow];
if (category.subcategories.count == 0) { 當沒有子數據的時候發送通知
2.發送通知
NSDictionary *userInfo = @{XMGCategoryNotificationKey : category};
[[NSNotificationCenter defaultCenter] postNotificationName:XMGCategoryNotification object:nil userInfo:userInfo];
}
}(void)lrTableView:(XMGLRTableView *)lrTableView seletedLeftRow:(NSInteger)leftRow seletedRightRow:(NSInteger)rightRow {
1.獲取數據
XMGCategory *category = self.categoryData[leftRow];
NSString *subCategory = category.subcategories[rightRow];
2.發送通知
NSDictionary *categoryUserInfo = @{XMGCategoryNotificationKey : category,
XMGSubCategoryNotificationKey : subCategory};
[[NSNotificationCenter defaultCenter] postNotificationName:XMGCategoryNotification object:nil userInfo:categoryUserInfo];
}三.接收通知改變內容
pragma mark - 監聽通知
- (void)setupNotifications {
1.類別通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(categoryChange:) name:XMGCategoryNotification object:nil];
2.區域通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(regionChange:) name:XMGRegionNotification object:nil];
}
pragma mark - 類別發生改變
-
(void)categoryChange:(NSNotification *)noti {
1.從字典中取出數據
XMGCategory *category = noti.userInfo[XMGCategoryNotificationKey];
NSString *subCategory = noti.userInfo[XMGSubCategoryNotificationKey];2.設置標題和子標題顯示
2.1 獲取itemView
XMGTopItemView *topItemView = self.categoryItem.customView;
if (!category.subcategories) { 沒有子類別
[topItemView setTitle:@"美團"];
[topItemView setSubTitle:category.name];
} else { 有子類別
[topItemView setTitle:category.name];
[topItemView setSubTitle:subCategory];
}設置圖片
[topItemView setIconWithimage:category.icon highImage:category.highlighted_icon];關閉popver
[self.categoryVC dismissViewControllerAnimated:YES completion:nil];設置可交互
[self barButtonEnabled];
}
pragma mark - 區域發生改變
-
(void)regionChange:(NSNotification *)noti {
1.從字典中取出數據
XMGRegion *region = noti.userInfo[XMGRegionNotificationKey];
NSString *subRegion = noti.userInfo[XMGSubRegionNotificationKey];2.設置標題和子標題顯示
2.1 獲取itemView
XMGTopItemView *topItemView = self.regionItem.customView;
if (!region.subregions) { 沒有子類別
[topItemView setTitle:@"廣州"];
[topItemView setSubTitle:@"全部區域"];
} else { 有子類別
[topItemView setTitle:region.name];
[topItemView setSubTitle:subRegion];
}3.彈出popVer
[self.regionVC dismissViewControllerAnimated:YES completion:nil];4.設置可交互
[self barButtonEnabled];
}
排序的Popover和item點擊的bug
- 一 XMGSortController
-
(void)viewDidLoad {
[super viewDidLoad];1.設置背景顏色
self.view.backgroundColor = [UIColor whiteColor];2.設置內容
CGFloat width = 120;
CGFloat height = 40;
CGFloat left = 20;
CGFloat topMargin = 10;
for (int i = 0; i < self.sorts.count; i++) {
XMGSort *sort = self.sorts[i];
UIButton sortBtn = [[UIButton alloc] init];
sortBtn.tag = i;
[sortBtn setTitle:sort.label forState:UIControlStateNormal];
[sortBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[sortBtn setBackgroundImage:[UIImage imageNamed:@"btn_filter_normal"] forState:UIControlStateNormal];
[sortBtn setBackgroundImage:[UIImage imageNamed:@"btn_filter_selected"] forState:UIControlStateSelected];
[sortBtn addTarget: self action:@selector(sortBtnClick:) forControlEvents:UIControlEventTouchUpInside];
sortBtn.frame = CGRectMake(left, (height+topMargin)i + topMargin, width, height);
[self.view addSubview:sortBtn];
}3.設置內容控制器的size
self.preferredContentSize = CGSizeMake(width + 2left, self.sorts.count(height+topMargin) + topMargin);
} -
(void)sortBtnClick:(UIButton *)sortBtn {
1.切換選中狀態
self.seletedBtn.selected = NO;
self.seletedBtn = sortBtn;
self.seletedBtn.selected = YES;2.發送通知
XMGSort *sort = self.sorts[sortBtn.tag];
NSDictionary *userInfo = @{XMGSortNotificationKey : sort};
[[NSNotificationCenter defaultCenter] postNotificationName:XMGSortNotification object:nil userInfo:userInfo];
}
pragma mark - 懶加載
(NSArray *)sorts {
if (_sorts == nil) {
self.sorts = [XMGSort objectArrayWithFilename:@"sorts.plist"];
}
return _sorts;
}二.
-
(void)sortChange:(NSNotification *)noti {
1.從字典中取出數據
XMGSort *sort = noti.userInfo[XMGSortNotificationKey];2.更改標題
2.1獲取itemView
XMGTopItemView *itemView = self.sortItem.customView;
[itemView setSubTitle:sort.label];3.退出popVer
[self.sortVC dismissViewControllerAnimated:YES completion:nil];4.設置可交互
[self barButtonEnabled];
}
UISplitViewController
- 一MenuViewController
pragma mark - tableView datasource
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.foodTypes.count;
}-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}1.取出模型
FoodType *ft = self.foodTypes[indexPath.row];
2.給cell設置數據
cell.textLabel.text = ft.name;
return cell;
}
pragma mark - tableView delegate
-
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
1.取出模型
FoodType *ft = self.foodTypes[indexPath.row];2.發送通知
NSDictionary *userInfo = @{menuDidChangeNotificationKey : ft};
[[NSNotificationCenter defaultCenter] postNotificationName:menuDidChangeNotification object:nil userInfo:userInfo];
}
pragma mark - 懶加載
(NSArray *)foodTypes {
if (_foodTypes == nil) {
_foodTypes = [FoodType objectArrayWithFilename:@"food_types.plist"];
}
return _foodTypes;
}二 DetailViewController
-
(void)viewDidLoad {
[super viewDidLoad];1.設置高度
self.tableView.rowHeight = 80;2.接收通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuChange:) name:menuDidChangeNotification object:nil];
}
pragma mark - 接收通知刷新表格
- (void)menuChange:(NSNotification *)noti {
1.從字典中取模型
FoodType *ft = noti.userInfo[menuDidChangeNotificationKey];
2.加載對應的plist
self.foods = [Food objectArrayWithFilename:[NSString stringWithFormat:@"type_%@_foods.plist",ft.idstr]];
3.刷新數據源
[self.tableView reloadData];
4.改變title
self.title = ft.name;
}
pragma mark - Table view data source
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.foods.count;
}-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}1.取出模型
Food *fd = self.foods[indexPath.row];2.設置數據
cell.textLabel.text = fd.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"時間:%@ 難度%@",fd.time,fd.diff];return cell;
}
pragma mark - Table view delegate
-
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
1.獲取模型數據
Food *fd = self.foods[indexPath.row];2.彈出控制器
RecipeViewController *recipeVC = [[RecipeViewController alloc] init];
recipeVC.fd = fd;
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:recipeVC];
nav.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:nav animated:YES completion:nil];
}
pragma mark - 移除通知
(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}三 RecipeViewController
(void)loadView {
更改控制器的View
UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = webView;
self.webView = webView;
}-
(void)viewDidLoad {
[super viewDidLoad];1.設置導航條左側內容
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"關閉" style:UIBarButtonItemStyleDone target:self action:@selector(exit)];2.設置標題
self.title = self.fd.name;3.webView展示信息
NSString *urlString = [NSString stringWithFormat:@"Html/food/%@.html", self.fd.idstr];
NSURL *url = [[NSBundle mainBundle] URLForResource:urlString withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
- (void)exit {
[self dismissViewControllerAnimated:YES completion:nil];
}