UIPickerView在UIPopoverController中正確顯示方法

來源:互聯網
上載者:User

開發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裡的內容自行修改。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.