QQ空间HD(2)-UIPopoverController其它使用
DJTestViewController.m
#import "DJTestViewController.h" #import "DJColorTableViewController.h" @interface DJTestViewController() <DJColorControllerDelegate> @property (nonatomic,weak) UIButton *btn1; @property (nonatomic,weak) UIButton *btn2; @property (nonatomic,strong) UIPopoverController *popVC; @end @implementation DJTestViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; UIButton *btn1 = [[UIButton alloc] init]; btn1.frame = CGRectMake(0, 0, 100, 50); [btn1 setTitle:@"测试" forState:UIControlStateNormal]; [btn1 setBackgroundColor:[UIColor greenColor]]; [self.view addSubview:btn1]; self.btn1 = btn1; [btn1 addTarget:self action:@selector(btn1DidClick:) forControlEvents:UIControlEventTouchUpInside]; UIButton *btn2 = [[UIButton alloc] init]; btn2.backgroundColor = [UIColor redColor]; btn2.frame = CGRectMake(200, 200, 50, 50); [btn2 setTitle:@"测试2" forState:UIControlStateNormal]; [btn2 addTarget:self action:@selector(btn2DidClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn2]; self.btn2 = btn2; } - (void)btn1DidClick:(UIButton *)btn { DJColorTableViewController *colorVC = [[DJColorTableViewController alloc] init]; colorVC.delegate = self; UIPopoverController *popVC = [[UIPopoverController alloc] initWithContentViewController:colorVC]; // 使popover在弹出时btn2仍然可以被点击 popVC.passthroughViews = @[self.btn2]; [popVC presentPopoverFromRect:CGRectMake(0, 0, 100, 50) inView:[self.btn1 superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; self.popVC = popVC; } - (void)btn2DidClick:(UIButton *)btn { NSLog(@"btn2 被点击"); } // 当popover 里面的CELL被点击时调用此方法 - (void)colorTableViewController:(DJColorTableViewController *)vc didSelectedColor:(UIColor *)color { self.btn1.backgroundColor = color; [self.popVC dismissPopoverAnimated:YES]; } @end
DJColorTableViewController.h
#import <UIKit/UIKit.h> @class DJColorTableViewController; @protocol DJColorControllerDelegate <NSObject> @optional - (void)colorTableViewController:(DJColorTableViewController *)vc didSelectedColor:(UIColor *)color; @end @interface DJColorTableViewController : UITableViewController @property (nonatomic,weak) id<DJColorControllerDelegate> delegate; @end
DJColorTableViewController.m
#import "DJColorTableViewController.h" #define DJRandomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0]; static NSUInteger numberOfROws = 4; @implementation DJColorTableViewController - (void)viewDidLoad { [super viewDidLoad]; // 取消tableview cell分隔线 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // 内部决定将来显示在popover里面的宽度和高度 self.preferredContentSize = CGSizeMake(320, 44 * numberOfROws); } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return numberOfROws; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"color"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } cell.backgroundColor = DJRandomColor; // 取消cell选中状态 cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UIColor *backgroundColor = [tableView cellForRowAtIndexPath:indexPath].backgroundColor; if ([self.delegate respondsToSelector:@selector(colorTableViewController:didSelectedColor:)]) { [self.delegate colorTableViewController:self didSelectedColor:backgroundColor]; } } @end
UIPopoverController 需要掌握的其它要点:
1. 使popover里面的controller可以自行决定其宽高:
在本例中popover里面的controller就是DJColorTableViewController
self.preferredContentSize = CGSizeMake(320, 44 * numberOfROws);
2. 让popover里面的发生的事件可以告诉外面(代理、通知)
3. 使popover消失
[self.popVC dismissPopoverAnimated:YES];
4. 在popover的蒙版弹出时,蒙版后面的按钮仍然可以被点击
// 使popover在弹出时btn2仍然可以被点击 popVC.passthroughViews = @[self.btn2];
5. 使popover可以在任意View的位置被弹出,不仅仅是UIBarButtonItem
[popVC presentPopoverFromRect:CGRectMake(0, 0, 100, 50) inView:[self.btn1 superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
方法说明:popover会在指定View的指定位置弹出。
第一个参数frame代表指定的位置。
第二个参数View代表相对于哪个View。
当然了,如果想在btn1 view的下方显示,最简便的写法如下:
[popVC presentPopoverFromRect:self.btn1.bounds inView:self.btn1 permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
苹果这样设计可以方便popover从任何位置弹出。