我的表单中有textfield
。当输入textfield
时,我希望弹出窗口中出现pickerView
。我之前使用过popOverController
,但在这种情况下,我不知道如何放置pickerView
并将其委托和数据源设置为表单查看控制器,因为实际上popover需要一个独立的视图控制器。
我尝试了以下(应用程序是通用的),popover没有出现:
- (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 :(得分: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
}