重点 (三) : UIPopoverController

1.png

使用步骤

要想显示一个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 :是否通过动画显示出来
2.png

设置显示的位置

• 如果想让箭头指向某一个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;
      }
封装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];
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容