2009-05-25 57 views

回答

25

编辑:这个答案是正确的,在写作的时候。苹果自此推出inputView。 Mafonya answer是你现在应该做的事情。

可以防止键盘弹出。将您的班级设置为UITextField的代表:textField.delegate = self并在您的界面声明之后(在标头文件中的{)之前添加:<UITextFieldDelegate>

现在实行textFieldShouldBeginEditing:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
    // Show UIPickerView 

    return NO; 
} 

如果你希望能够隐藏你的pickerView这种方法是行不通的。然后你可以做的是创建UITextField的子类并覆盖*trackingWithTouch:event:选择器,并在那些方面发挥你的魔力。您可能仍然需要从textFieldShouldBeginEditting:返回NO以防止显示键盘。

+1

您的解决方案部分工作。我想显示某些特定uitextfields上的选取器视图,而不是在所有文本字段上,以及从填充在所需文本字段中的选取器视图中选择值时,必须隐藏选取器视图。 – 2009-05-25 15:25:18

1

退房代码为(同时存在的TextView +的TabBar和pickerview +的TabBar代码)

UITextView and UIPickerView with its own UIToolbar

这将得到pickerview辞职等所有u需要做的就是用pickerview代理方法更新文本视图

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 

而不是使用文本字段,使用UIlabel,白色背景。这样,当你点击它时键盘不会显示。重写UILabel上的触摸事件,当发生这种情况时,调用该方法来形成显示新视图的私有问题。

-(void) addToViewWithAnimation:(UIView *) theView 
+0

我不想键盘显示只有选择器的观点是,我想,当我在文本字段 – 2009-05-25 10:14:00

+0

耶键盘将几乎总是显示进入显示。您将不得不使用其他视图,如uiLabel。我已经编辑了更简洁的答案。 – Bluephlame 2009-05-25 10:54:20

+0

正如蓝色所提到的,键盘会一直显示出来,你无法避免它。另一种方法是在键盘上添加一个选择器视图。它们的高度相同,所以键盘不会被看到。 – lostInTransit 2009-05-25 11:29:24

4

嘿,我已经粘贴了一些代码,我写了一段时间来回顾这个场景。有两个例子一项所述的带actionsheet和一个没有actionsheet:

- (void)textFieldDidBeginEditing:(UITextField *)myTextField{ 

      [myTextField resignFirstResponder]; 

      actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 

      [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; 

      CGRect pickerFrame = CGRectMake(0, 40, 0, 0); 

      UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame]; 

       pickerView.showsSelectionIndicator = YES; 

       pickerView.dataSource = self; 

       pickerView.delegate = self; 

       [actionSheet addSubview:pickerView]; 

       [pickerView release]; //NB this may result on the pickerview going black the next time you come back to the textfield, if this is the case just remove this statement 

       UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"SUBMIT", @"")]]; 

       closeButton.momentary = YES; 

       closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f); 

       closeButton.segmentedControlStyle = UISegmentedControlStyleBar; 

       closeButton.tintColor = [UIColor blackColor]; 

       [closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged]; 

       [actionSheet addSubview:closeButton]; 

       [closeButton release]; 

       [actionSheet showInView:displayedInView]; 

       [actionSheet setBounds:CGRectMake(0, 0, 320, 485)]; 

} 

这是没有actionsheet的代码:http://www.buggyprogrammer.co.uk/2010/08/18/open-uipickerview-when-user-enters-textfield/

- (void)textFieldDidBeginEditing:(UITextField *)myTextField{ 

     [myTextField resignFirstResponder]; 

      for (int component = 0; component &lt; (((NSInteger)numberOfComponentsForPickerView) - 1); component++) { 

       NSInteger valueForRow = [[self.textField.text substringWithRange:NSMakeRange(component,1)] integerValue]; 

       [pickerView selectRow:valueForRow inComponent:component animated:YES]; 

      } 

      [self fadeInPickerView]; 

      [view addSubview:pickerView]; 

      [pickerView release]; 

} 

这方面的一个更详细的示例,可以发现

4

附加inputview到文本框。

picker = [[UIPickerView alloc]init]; 
[picker setDataSource:self]; 
[picker setDelegate:self]; 
[picker setShowsSelectionIndicator:YES]; 

MyTextField.inputView = picker;