您的位置:首页 > 产品设计 > UI/UE

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里的内容自行修改。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: