2013-04-07 70 views
1

我有一個textfield在我的表單。當打開textfield時,我想要一個彈出窗口,內部顯示pickerView。我之前使用過popOverController,但在這種情況下,我不知道如何放置pickerView,並將它的委託和數據源設置爲表單視圖控制器,因爲實際上彈出窗口需要獨立的視圖控制器。UIPickerView窗體內的一個彈出窗口viewController(iPad)

我嘗試以下(應用程序是通用的),該酥料餅沒有出現:

- (IBAction)gradeTextfieldPressed:(UITextField *)sender 
{ 

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 

     [self.nameTextField resignFirstResponder]; 
     UIViewController *pickerController = [[UIViewController alloc] init]; 
     UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(self.gradeTextField.center.x, self.gradeTextField.center.y, 320, 320)]; 
     pickerView.delegate = self; 
     pickerView.dataSource = self; 
     [pickerController.view addSubview:pickerView]; 
     UIPopoverController *pickerPopover = [[UIPopoverController alloc] initWithContentViewController:pickerController]; 
     [pickerPopover presentPopoverFromRect:CGRectMake(self.gradeTextField.center.x, self.gradeTextField.center.y, 320, 320) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; 
     [self.gradeTextField setInputView:pickerView]; 
     if ([self.gradeTextField.text isEqualToString:@""]) { 
      [self pickerView:pickerView didSelectRow:0 inComponent:0]; 
     } 

    } else if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){ 
     [self.picker setHidden:NO]; 
     //[self pickerView:self.picker didSelectRow:0 inComponent:0]; 
     if ([self.gradeTextField.text isEqualToString:@""]) { 
      [self pickerView:self.picker didSelectRow:0 inComponent:0]; 
     } 
     [self.gradeTextField setInputView:self.picker]; 
     [self.nameTextField resignFirstResponder]; 
     [self.creditstextField resignFirstResponder]; 
     [self.chaptersTextField resignFirstResponder]; 
    }  
} 
+0

我檢查它,它似乎是誰貼吧沒找不到他的問題的解決方案:/ – HusseinB 2013-04-07 18:54:30

回答

0

的一種方式做到這一點,我發現簡單的創建它只是PickerView一個新的控制器(以這種情況下使用.xib),並有一個簡單的委託協議,讓表單控制器知道什麼時候被挑選。

@protocol MyPickerDelegate <NSObject> 
- (void)valuePicked:(NSString*)value 
@end 

@interface MyPickerController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> 
{ 
    __weak IBOutlet UIPickerView *_pickerView; 
} 
@property (nonatomic, weak) id<MyPickerDelegate> delegate; 

然後,您可以在此控制器中實施數據源和委託方法。這裏將是該項目被選中時的代碼:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    [_delegate valuePicked:[_myStrings objectAtIndex:row]]; 
} 

最後,在調用的類(表單表控制器):

- (void)displayPickerFromRect:(CGRect)rect inView:(UIView*)view 
{ 
    if (!_pickerController) 
     _pickerController = [[MyPickerController alloc] initWithNibName:nil bundle:nil]; 

    if (_pickerController) 
    { 
     _speedPickerController.delegate = self; 

     if (!_pickerPopover) 
     { 
      _pickerPopover = [[UIPopoverController alloc] initWithContentViewController:_pickerController]; 
      _pickerPopover.popoverContentSize = _pickerController.view.frame.size; 
      _pickerPopover.delegate = self; 
     } 

     [_pickerPopover presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
    } 
} 

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController 
{ 
    if (_pickerController) 
    { 
     _pickerController = nil; 
     _pickerPopover = nil; 
    } 
} 

- (void)valuePicked:(NSString*)value 
{ 
    // do something with the picked value 
    // dismiss the popover if you want it to go away as soon as a value is picked 
} 
+0

我發佈了一些我試過的請看看上面,謝謝! – HusseinB 2013-04-08 18:33:13