Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ios中单击UITextView打开Pickerview(下拉菜单)?_Ios_Objective C_Drop Down Menu_Uitextfield_Uipickerview - Fatal编程技术网

如何在ios中单击UITextView打开Pickerview(下拉菜单)?

如何在ios中单击UITextView打开Pickerview(下拉菜单)?,ios,objective-c,drop-down-menu,uitextfield,uipickerview,Ios,Objective C,Drop Down Menu,Uitextfield,Uipickerview,我正在做一个项目,我想添加一个下拉列表来选择类别。 请检查屏幕截图(目前仅限于其UITextField)。 当用户单击UITextField时,如何在屏幕底部显示PickerView 我试过一些代码,但都不起作用 - (BOOL) textFieldShouldBeginEditing:(UITextView *)textView { pickrView.frame = CGRectMake(0, 400,self.view.frame.size.width,300);

我正在做一个项目,我想添加一个下拉列表来选择类别。 请检查屏幕截图(目前仅限于其
UITextField
)。

当用户单击
UITextField
时,如何在屏幕底部显示PickerView

我试过一些代码,但都不起作用

    - (BOOL) textFieldShouldBeginEditing:(UITextView *)textView
{
    pickrView.frame = CGRectMake(0, 400,self.view.frame.size.width,300);
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.50];
    [UIView setAnimationDelegate:self];
    pickrView.frame = CGRectMake(0, 200, pickrView.frame.size.width, pickrView.frame.size.height);
    [self.view addSubview:pickrView];
    [UIView commitAnimations];
    return NO;
}

单击textfield时,您需要一个pickerview而不是键盘。因此,在创建textfield时,必须将pickerview分配给UITextField的“inputview”属性

让我举个例子来解释一下

首先创建文本字段

- (void)viewDidLoad
{
    [super viewDidLoad];
     UITextField *cellTextField = [[UITextField alloc] init];
     cellTextField.frame = CGRectMake(20, 12, 180, 20);
     cellTextField.inputView = [self createPicker];
}
创建PickerView

- (UIView *)createPicker {

    UIView *pickerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 216)];
    pickerView.backgroundColor = [UIColor colorWithRed:109.0/255.0 green:110.0/255.0 blue:120.0/255.0 alpha:1];

    UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 45, 300, 120)];
    picker.delegate = self;
   [pickerView addSubview:picker];

   return pickerView;
}


点击文本字段时,您将获得pickerview。

pickerview是否已添加[self.view addSubview:pickerview];在动画之前添加