今天一位童鞋问我个问题。大意是popoverController不会显示。经过我寻找问题发现以下这种方法不好掌控。

为什么说他不好掌控那。我这个给大家带来一个列子。通过这个列子来介绍PopoverController的具体使用方法,以及这种方法的2中传參技巧。

- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
animated:(BOOL)animated;

方案1:新建一个view,在这个view上加入手势来监听PopoverController的弹出。

监听方法例如以下:

- (IBAction)tapClick:(UITapGestureRecognizer *)sender {
ColorViewController *color = [[ColorViewController alloc]init]; _pc = [[UIPopoverController alloc] initWithContentViewController:color];
_pc.popoverContentSize = CGSizeMake(320, 480); [_pc presentPopoverFromRect:sender.view.bounds inView:sender.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

请大家细致看:这里的sender仅仅是一个手势。

上面的sender.view是谁?就是手势被加入的视图。fromRect这个传的就是手势被加入的到哪个视图就传哪个视图。后边的inview意思就是popover在哪个视图?当然是在手势被加入的视图了。

方案2:新建一个button,在这个button上加入手势来监听PopoverController的弹出

- (IBAction)btnClick:(UIButton *)sender
{
ColorTableViewController *color = [[ColorTableViewController alloc]init]; _pc = [[UIPopoverController alloc] initWithContentViewController:color];
_pc.popoverContentSize = CGSizeMake(320, 480); [_pc presentPopoverFromRect:sender.bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

这里的sender指的是button。fromRect传的是button的尺寸,后面的inview传的是button本身。道理同上。

3.新建一个controller继承UITableViewController

UIPopoverController具体解释-LMLPHP

4.编写数据源方法

#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ return 20;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"第%d行数据",indexPath.row];
CGFloat red = arc4random_uniform(255) / 255.0;
CGFloat green = arc4random_uniform(255) / 255.0;
CGFloat black = arc4random_uniform(255) / 255.0;
cell.contentView.backgroundColor = [UIColor colorWithRed:red green:green blue:black alpha:1]; return cell;
}

5.效果图

UIPopoverController具体解释-LMLPHP

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWlucWlhbmdxaWFuZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

05-11 13:48