UIPickerView在UIPopoverController中正确显示方法
2013-05-28 17:09
375 查看
开发iPad应用程序与iPhone有一点小差别,就是iPad支持弹出框。这个示例展示如何在UIPopoverController上显示一个UIPickerView,当然你可以显示任何的UIView到UIPopover上面。原理就是构建一个UIViewController,然后将这个UIViewController加在UIPopoverController上,最近显示UIPopoverController,即显示出我们的UIViewController的内容。
需要说明的是,
picker的高度目前是改不了的, 就是216(我自己打印得出来的), 网上有一些效果其实并非改的高度
首先,要我们的controller支持UIPickerViewDelegate,UIPopoverControllerDelegate协议,
@interface myViewController : UIViewController<UIPickerViewDelegate,UIPopoverControllerDelegate>
然后开始显示uipickerview
- (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:
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
[popViewController release];
}
还有实现UIPickerDelegate
- (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里的内容自行修改。
需要说明的是,
picker的高度目前是改不了的, 就是216(我自己打印得出来的), 网上有一些效果其实并非改的高度
首先,要我们的controller支持UIPickerViewDelegate,UIPopoverControllerDelegate协议,
@interface myViewController : UIViewController<UIPickerViewDelegate,UIPopoverControllerDelegate>
然后开始显示uipickerview
- (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:
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
[popViewController release];
}
还有实现UIPickerDelegate
- (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里的内容自行修改。
相关文章推荐
- UIPickerView在UIPopoverController中正确显示方法
- UIPickerView在UIPopoverController中正确显示方法
- UIPickerView在UIPopoverController中正确显示方法
- UIPickerView的属性和常用方法 举例:显示省份和城市
- UiPickerView基本使用方法
- UIPickView的显示与消失方法
- presentViewController显示方式的窗口,没有导航栏的解决方法
- iOS系统UIPickerView的简单使用和方法属性介绍
- PresentModalViewController 的正确使用方法
- iOS 一个ViewController上显示2个tableView的方法
- UIAlertController + UIPickerView完美结合
- 【Android 开发】:UI控件之显示图片控件 ImageView 的使用方法
- iOS 修改UIDocumentPickerViewController navigation bar buttons color
- MPMoviePlayerViewController的使用方法和横屏显示
- UI: 使用 UIActivityViewController 显示分享选项
- ipad开发中UIPopoverController中显示自定义view尺寸显示问题
- UIPickerView控件中自定义显示的字体大小及样式
- 错误显示:2012-03-09 17:20:22.090 viewcontrollerjump[1191:207] *** Assertion failure in -[UIWindowController transition:fromViewController:toViewController
- UIPickerView 显示与隐藏
- UI: 使用 UIActivityViewController 显示分享选项