UIPopoverController的使用
1 实现如下:
2 首先加入委托:UIPopoverControllerDelegate
3 先创建一个popover和实例化tableview的类(该类的xib文件中,view中就放了一个tablview):
4 UIPopoverController *popover;
5 popTableViewController *popoverContent;
6
7 在CustomerDetailVC.m放入:
8 #import "CustomerDetailVC.h"
9 #import "popTableViewController.h"
10 @implementation CustomerDetailVC
11 //点击Country后面按钮的时候,弹出popover:
12 -(IBAction)btnPressed:(id)sender
13 {
14 popoverContent.oceanaViewController = self;
15
16 popover = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
17 [popover setPopoverContentSize:CGSizeMake(330,330)];
18 popoverContent.contentSizeForViewInPopover=popoverContent.view.bounds.size;
19 popoverContent.popoverController = popover;
20 [popover presentPopoverFromRect:((UIView *)sender).frame
21 inView:self.view
22 permittedArrowDirections:UIPopoverArrowDirectionAny
23 animated:YES];
24 [popoverContent release]; //在这里popoverContent的retainCount =4;
25 [popoverContent.myArray release];
26 }
27 - (void)popoverControllerDidDismissPopover:(UIPopoverController*)popoverController{
28 if (popover) {
29 [popover dismissPopoverAnimated:YES];
30 [popover release];
31 popover=nil;
32 if (popoverContent != nil) {
33 [popoverContent release];
34 popoverContent = nil;
35 }
36 }
37 }
38 //closing popover
39 -(void)killPopoversOnSight {
40 if (popover) {
41 [popover dismissPopoverAnimated:NO];
42 }
43 }
44 在popTableViewController.h文件中:
45 #import "CustomerDetailVC.h"
46 @class CustomerDetailVC;
47 @interface popTableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> {
48 IBOutlet UITableView *myTableView;
49 NSMutableArray *myArray;
50 UIPopoverController *popoverController;
51 CustomerDetailVC *oceanaViewController;
52 NSString *selectStr;
53
54 }
55
56 @property (nonatomic,retain) UITableView *myTableView;
57 @property (nonatomic,retain) NSMutableArray *myArray;
58 @property (nonatomic,retain) UIPopoverController *popoverController;
59 @property (nonatomic,retain) NSString *selectStr;
60 @property (nonatomic,assign) CustomerDetailVC *oceanaViewController;
61 实现文件中:
62 #import "popTableViewController.h"
63 @implementation popTableViewController
64 @synthesize myTableView;
65 @synthesize selectStr;
66 @synthesize myArray;
67 @synthesize popoverController;
68 @synthesize oceanaViewController;
69
70
71 -(void)viewDidLoad
72 {
73 [myTableView setDelegate:self];
74 [myTableView setDataSource:self];
75 }
76 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
77 return @"Name";
78 }
79 //指定有多少个分区(Section),默认为1
80 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
81 return 1;
82 }
83
84 //指定每个分区中有多少行,默认为1
85 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
86 return [myArray count];
87 }
88 //绘制Cell
89 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
90
91 static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
92 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
93 if (cell == nil) {
94 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier] autorelease];
95 }
96 cell.textLabel.text =[myArray objectAtIndex:indexPath.row];
97 return cell;
98 }
99 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
100 selectStr =[[NSString alloc] initWithFormat:@"%@",[myArray objectAtIndex:indexPath.row]];
101 [oceanaViewController killPopoversOnSight];
102 [oceanaViewController textGetValue:selectStr]; //CustomerDetailVC中的一个方法
103 [selectStr release];
104 }
////////////////
UIPopoverController只能在ipad设备上面使用;作用是用于显示临时内容,特点是总是显示在当前视图最前端,当单击界面的其他地方时自动消失。
1 UIViewController* vCtrl = [[UIViewController alloc] init];
2 UIPopoverController* popController = [[UIPopoverController alloc] initWithContentViewController:vCtrl];
3
4 popController.popoverContentSize = CGSizeMake(320,480);
5 CGrect rect = CGRectMake(0,0,2,2);
6
7 [popController presentPopoverFromRect:rect
8 inView:aView
9 permittedArrowDirections:UIPopoverArrowDirectionUp
10 animated:YES];
在使用该控制器的时候,下面几个步骤是必须的:
1.UIPopoverController该控制器的内容必须由一个控制器提供;提供方式有三:
- - (id)initWithContentViewController:(UIViewController *)viewController
- @property (nonatomic, retain) UIViewController *contentViewController
- - (void)setContentViewController:(UIViewController *)viewController animated:(BOOL)animated
2.设置箭头方向:
@property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection
3.设置内容大小:
@property (nonatomic) CGSize popoverContentSize
备注:
<1>.如果从一个导航按钮处呈现,使用:
presentPopoverFromBarButtonItem:permittedArrowDirections:animated:;
如果要从一个视图出呈现,使用:
presentPopoverFromRect:inView:permittedArrowDirections:animated:
<2>.如果设备旋转以后,位置定位错误需要在父视图控制器的下面方法里面重新定位:
didRotateFromInterfaceOrientation:(在这个方法体里面重新设置rect)
然后再次调用:
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated