UIPopoverController 使用

开发iPad应用程序与iPhone有一点小差别,就是iPad支持弹出框。这个示例展示如何在UIPopoverController上显示一个UIPickerView,当然你可以显示任何的UIView到UIPopover上面。原理就是构建一个UIViewController,然后将这个UIViewController加在UIPopoverController上,最近显示UIPopoverController,即显示出我们的UIViewController的内容。

首先,要我们的controller支持UIPickerViewDelegate,UIPopoverControllerDelegate协议,

 

  1. @interface myViewController : UIViewController<UIPickerViewDelegate,UIPopoverControllerDelegate>  
@interface myViewController : UIViewController<UIPickerViewDelegate,UIPopoverControllerDelegate>


然后开始显示uipickerview

 

 

  1. - (void)showPickerInPopover:(CGRect)rect  
  2. {  
  3.       
  4.     UIViewController *sortViewController = [[UIViewController alloc] init];  
  5.     UIView *theView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];  
  6.     UIPickerView *thePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];  
  7.     thePicker.delegate = self;  
  8.     thePicker.dataSource = self;  
  9.     thePicker.showsSelectionIndicator = YES;  
  10.     [theView addSubview:thePicker];  
  11.     sortViewController.view = theView;  
  12.     [theView release];  
  13.       
  14.     popViewController = [[UIPopoverController alloc] initWithContentViewController:sortViewController];  
  15.     [popViewController setPopoverContentSize:CGSizeMake(320, 216) animated:NO];  
  16.     [popViewController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];  
  17.     popViewController.delegate = self;  
  18.     ;  
  19.     [sortViewController release];  
  20. }  
- (void)showPickerInPopover:(CGRect)rect
{
	
	UIViewController *sortViewController = [[UIViewController alloc] init];
	UIView *theView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
	UIPickerView *thePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
	thePicker.delegate = self;
	thePicker.dataSource = self;
	thePicker.showsSelectionIndicator = YES;
	[theView addSubview:thePicker];
	sortViewController.view = theView;
	[theView release];
    
    popViewController = [[UIPopoverController alloc] initWithContentViewController:sortViewController];
	[popViewController setPopoverContentSize:CGSizeMake(320, 216) animated:NO];
    [popViewController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
	popViewController.delegate = self;
	;
    [sortViewController release];
}


注意内存管理,要释放UIPopoverController:

 

 

  1. - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController  
  2. {  
  3.     [popViewController release];  
  4. }  
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
	[popViewController release];
}


还有实现UIPickerDelegate

 

 

  1. - (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {  
  2.     // Handle the selection   
  3. }  
  4.    
  5. // tell the picker how many rows are available for a given component   
  6. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {  
  7.     NSUInteger numRows = 5;  
  8.    
  9.     return numRows;  
  10. }  
  11.    
  12. // tell the picker how many components it will have   
  13. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {  
  14.  return 1;  
  15. }  
  16.    
  17. // tell the picker the title for a given component   
  18. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {  
  19.     NSString *title;  
  20.     title = [@"" stringByAppendingFormat:@"%d",row];  
  21.    
  22.     return title;  
  23. }  
  24.    
  25. // tell the picker the width of each row for a given component   
  26. - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {  
  27.  int sectionWidth = 300;  
  28.    
  29.  return sectionWidth;  
  30. }  
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    // Handle the selection
}
 
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSUInteger numRows = 5;
 
    return numRows;
}
 
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
 return 1;
}
 
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *title;
    title = [@"" stringByAppendingFormat:@"%d",row];
 
    return title;
}
 
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
 int sectionWidth = 300;
 
 return sectionWidth;
}


UIPickerView里的内容自行修改。

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

系统优化的我一愣一愣的,原来是下面的优化规则

 

TodoViewController *contentViewController = [[TodoViewController alloc] init];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:contentViewController];

navigationController.contentSizeForViewInPopover = CGSizeMake(100, 100); //内容大小

 

    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];

popover.popoverContentSize = CGSizeMake(300, 300); //弹出窗口大小,如果屏幕画不下,会挤小的。这个值默认是320x1100

 

CGRect popoverRect = CGRectMake(200, 700, 10, 10);

[popover presentPopoverFromRect:popoverRect  //popoverRect的中心点是用来画箭头的,如果中心点如果出了屏幕,系统会优化到窗口边缘

inView:self.view //上面的矩形坐标是以这个view为参考的

  permittedArrowDirections:UIPopoverArrowDirectionDown  //箭头方向

  animated:YES];

 

[contentViewController release];

[navigationController release];

 

//最佳实践,使用哪个view做参考,就以哪个view的bounds送进去就好了,箭头自动指向这个view的中心

posted @ 2013-05-08 18:16  高笑228  阅读(210)  评论(0编辑  收藏  举报