标签:
1 #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 - (void)didReceiveMemoryWarning { 91 [super didReceiveMemoryWarning]; 92 // Dispose of any resources that can be recreated. 93 } 94 95 @end
有做的不对的或者需要改正的地方还望朋友们多多指教,谢谢大家了!
UIPopoverPresentationController实现及交互响应
标签:
原文地址:http://www.cnblogs.com/garywong1949/p/5406428.html