iOS中UIPopoverPresentationController的用法

#import "ViewController.h"
 2 #import "contentViewController.h"//内容视图控制器
 3 #import "AAViewController.h"//点击popover交互推出的控制器
 4 
 5 
 6 @class AAViewController;
 7 @interface ViewController ()<UIPopoverPresentationControllerDelegate,UITableViewDataSource>
 8 {
 9     contentViewController *contentVC;
10 }
11 @property(nonatomic,strong)UIPopoverPresentationController *popover;
12 @property(nonatomic,strong)NSArray *array;//popover表视图的文字
13 @property(nonatomic,strong)UITableView *tableView;//popover的表视图
14 
15 @end
16 
17 @implementation ViewController
18 
19 - (NSArray*)array{//懒加载
20     if (!_array) {
21         _array = [NSArray array];
22     }
23     return _array;
24 }
25 
26 - (void)viewDidLoad {
27     [super viewDidLoad];
28     
29     UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(playInputClick:)];
30     self.navigationItem.rightBarButtonItem = button;//导航栏右侧按钮
31     
32     _array = @[@"迷雾",@"迷岛",@"暗影",@"神奇"];
33 }
34 - (void)playInputClick:(id)sender{
35     
36     contentVC = [contentViewController new];//初始化内容视图控制器
37     contentVC.preferredContentSize = CGSizeMake(110, 155);
38     contentVC.modalPresentationStyle = UIModalPresentationPopover;
39     
40     _tableView = [[UITableView alloc] init];
41     _tableView.frame = contentVC.view.frame;
42     _tableView.dataSource = self;
43     _tableView.delegate = self;
44     [contentVC.view addSubview:_tableView];
45    
46 
47     self.popover = contentVC.popoverPresentationController;//初始化一个popover
48     self.popover.delegate = self;
49     self.popover.barButtonItem = self.navigationItem.rightBarButtonItem;//设置popover的来源按钮
50     
51     [self presentViewController:contentVC animated:YES completion:nil];//推出popover
52 }
53 
54 - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
55     return UIModalPresentationNone;
56 }
57 
58 
59 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
60     return self.array.count;
61 }
62 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
63     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
64     if (cell == nil) {
65         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
66     }
67     
68     cell.textLabel.text = self.array[indexPath.row];
69     
70     return cell;
71 }
72 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
73     return 155 / 4.0;
74 }
75 
76 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
77     [UIView animateWithDuration:0.3 animations:^{
78         [self dismissViewControllerAnimated:contentVC completion:nil];
79     }];
80     
81     switch (indexPath.row) {
82         case 0:
83             [self.navigationController pushViewController:[AAViewController new] animated:YES];
84             break;
85         default:
86             break;
87     }
88 }
89 
90 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值