UIPopoverController的使用
对UIPopoverController的使用(2011-06-20 18:07:21)效果如下:
实现如下:
首先加入委托:UIPopoverControllerDelegate
先创建一个popover和实例化tableview的类(该类的xib文件中,view中就放了一个tablview):
UIPopoverController *popover;
popTableViewController *popoverContent;
在CustomerDetailVC.m放入:
#import "CustomerDetailVC.h"
#import "popTableViewController.h"
@implementation CustomerDetailVC
//点击Country后面按钮的时候,弹出popover:
-(IBAction)btnPressed:(id)sender
{
popoverContent.oceanaViewController = self;
popover =[[UIPopoverController alloc]initWithContentViewController:popoverContent];
[popoversetPopoverContentSize:CGSizeMake(330,330)];
popoverContent.contentSizeForViewInPopover=popoverContent.view.bounds.size;
popoverContent.popoverController = popover;
[popoverpresentPopoverFromRect:((UIView *)sender).frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
[popoverContentrelease]; //在这里popoverContent的retainCount =4;
[popoverContent.myArray release];
}
-(void)popoverControllerDidDismissPopover:(UIPopoverController*)popoverController{
if (popover){
[popoverdismissPopoverAnimated:YES];
[popoverrelease];
popover=nil;
if(popoverContent != nil) {
[popoverContent release];
popoverContent = nil;
}
}
}
//closing popover
-(void)killPopoversOnSight {
if (popover){
[popoverdismissPopoverAnimated:NO];
}
}
在popTableViewController.h文件中:
#import "CustomerDetailVC.h"
@class CustomerDetailVC;
@interface popTableViewController :UIViewController<UITableViewDelegate,UITableViewDataSource>{
IBOutletUITableView *myTableView;
NSMutableArray *myArray;
UIPopoverController *popoverController;
CustomerDetailVC *oceanaViewController;
NSString*selectStr;
}
@property (nonatomic,retain) UITableView *myTableView;
@property (nonatomic,retain) NSMutableArray *myArray;
@property (nonatomic,retain) UIPopoverController*popoverController;
@property (nonatomic,retain) NSString *selectStr;
@property (nonatomic,assign) CustomerDetailVC*oceanaViewController;
实现文件中:
#import "popTableViewController.h"
@implementation popTableViewController
@synthesize myTableView;
@synthesize selectStr;
@synthesize myArray;
@synthesize popoverController;
@synthesize oceanaViewController;
-(void)viewDidLoad
{
[myTableViewsetDelegate:self];
[myTableViewsetDataSource:self];
}
- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{
return@"Name";
}
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return1;
}
//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{
return[myArray count];
}
//绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {
staticNSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell ==nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZeroreuseIdentifier:SimpleTableIdentifier] autorelease];
}
cell.textLabel.text =[myArray objectAtIndex:indexPath.row];
returncell;
}
- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectStr=[[NSString alloc] initWithFormat:@"%@",[myArrayobjectAtIndex:indexPath.row]];
[oceanaViewController killPopoversOnSight];
[oceanaViewController textGetValue:selectStr];//CustomerDetailVC中的一个方法
[selectStrrelease];
}
UIPopoverController *popover;
popTableViewController *popoverContent;
在CustomerDetailVC.m放入:
#import "CustomerDetailVC.h"
#import "popTableViewController.h"
@implementation CustomerDetailVC
//点击Country后面按钮的时候,弹出popover:
-(IBAction)btnPressed:(id)sender
{
popoverContent.oceanaViewController = self;
popover =[[UIPopoverController alloc]initWithContentViewController:popoverContent];
[popoversetPopoverContentSize:CGSizeMake(330,330)];
popoverContent.contentSizeForViewInPopover=popoverContent.view.bounds.size;
popoverContent.popoverController = popover;
[popoverpresentPopoverFromRect:((UIView *)sender).frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
[popoverContentrelease]; //在这里popoverContent的retainCount =4;
[popoverContent.myArray release];
}
-(void)popoverControllerDidDismissPopover:(UIPopoverController*)popoverController{
if (popover){
[popoverdismissPopoverAnimated:YES];
[popoverrelease];
popover=nil;
if(popoverContent != nil) {
[popoverContent release];
popoverContent = nil;
}
}
}
//closing popover
-(void)killPopoversOnSight {
if (popover){
[popoverdismissPopoverAnimated:NO];
}
}
在popTableViewController.h文件中:
#import "CustomerDetailVC.h"
@class CustomerDetailVC;
@interface popTableViewController :UIViewController<UITableViewDelegate,UITableViewDataSource>{
IBOutletUITableView *myTableView;
NSMutableArray *myArray;
UIPopoverController *popoverController;
CustomerDetailVC *oceanaViewController;
NSString*selectStr;
}
@property (nonatomic,retain) UITableView *myTableView;
@property (nonatomic,retain) NSMutableArray *myArray;
@property (nonatomic,retain) UIPopoverController*popoverController;
@property (nonatomic,retain) NSString *selectStr;
@property (nonatomic,assign) CustomerDetailVC*oceanaViewController;
实现文件中:
#import "popTableViewController.h"
@implementation popTableViewController
@synthesize myTableView;
@synthesize selectStr;
@synthesize myArray;
@synthesize popoverController;
@synthesize oceanaViewController;
-(void)viewDidLoad
{
[myTableViewsetDelegate:self];
[myTableViewsetDataSource:self];
}
- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{
return@"Name";
}
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return1;
}
//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{
return[myArray count];
}
//绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {
staticNSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell ==nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZeroreuseIdentifier:SimpleTableIdentifier] autorelease];
}
cell.textLabel.text =[myArray objectAtIndex:indexPath.row];
returncell;
}
- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectStr=[[NSString alloc] initWithFormat:@"%@",[myArrayobjectAtIndex:indexPath.row]];
[oceanaViewController killPopoversOnSight];
[oceanaViewController textGetValue:selectStr];//CustomerDetailVC中的一个方法
[selectStrrelease];
}