UIPickerView在UIPopoverController中正确显示方法
2011-08-29 11:57
706 查看
开发iPad应用程序与iPhone有一点小差别,就是iPad支持弹出框。这个示例展示如何在UIPopoverController上显示一个UIPickerView,当然你可以显示任何的UIView到UIPopover上面。原理就是构建一个UIViewController,然后将这个UIViewController加在UIPopoverController上,最近显示UIPopoverController,即显示出我们的UIViewController的内容。
首先,要我们的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里的内容自行修改。
首先,要我们的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的属性和常用方法 举例:显示省份和城市
- iOS UIPickerView 可能会出现选中行不显示或者去掉上下两条横线的问题解决,
- iOS 修改UIDocumentPickerViewController navigation bar buttons color
- iOS UIPickerView 显示全国省市
- UIPickerView选择器的使用方法
- UIPickerView的方法和属性
- UIPickerView 地区解析 -- 全国省、市、区 plist 解析 -- 读取UIPickerView 当前显示内容
- ViewController响应显示相关的通知方法
- ios-day10-03(省份和城市联动。UIPickerView的使用、使用UIPickerView的reloadComponent:方法刷新某一列)
- presentViewController显示方式的窗口,没有导航栏的解决方法
- UI: 用 UISplitViewController 显示 Master-Detail 视图 ;使用 UIPageViewController 启用分页;使用 UIPopoverController 显示弹出画面(弹出框)
- UINavigationController+UIScrollView内容视图全屏显示的方法
- UiPickerView基本使用方法
- UISplitViewController 代理方法
- iOS系统UIPickerView的简单使用和方法属性介绍
- UIAlertController + UIPickerView完美结合
- UIPickerView控件中自定义显示的字体大小及样式