8

我正在创建警报应用程序。我想UIDatePickerUIActionSheet点击时显示UITextField

4

3 回答 3

8

为此,您需要实施UIActionShetDelegateUITextFieldDelegate 在其中textFieldDidBeginEditing显示操作表,例如:

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
  [self showAction];
}
-(void) showAction
{
  UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:@"Pick the date." delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select", nil];
  [asheet showInView:[self.view superview]];
  [asheet setFrame:CGRectMake(0, 117, 320, 383)];
  [asheet release];
}

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
  {

    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];

   //Configure picker...
   [pickerView setMinuteInterval:5];
   [pickerView setTag: kDatePickerTag];

  //Add picker to action sheet
  [actionSheet addSubview:pickerView];

  [pickerView release];

  //Gets an array af all of the subviews of our actionSheet
  NSArray *subviews = [actionSheet subviews];

  [[subviews objectAtIndex:SelectButtonIndex] setFrame:CGRectMake(20, 266, 280, 46)];
  [[subviews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 317, 280, 46)];

}
于 2012-08-23T16:40:30.923 回答
5

我发现在使用带有选择器的文本字段时,最简单的方法是设置文本字段的输入视图并添加您自己的工具栏以方便使用。确保你有你的“UITextFieldDelegate”和选择器方法设置然后像这样连接它。

 yourtextfield.delegate = self;
 [yourtextfield setInputView:yourpickerview];
 [yourtextfield setInputAccessoryView:doneBar];

 doneBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
 [doneBar setBarStyle:UIBarStyleBlackTranslucent];
 UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] 
                initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                target:nil
                action:nil];
 [doneBar setItems: [NSArray arrayWithObjects:spacer2, [[UIBarButtonItem alloc] 
                    initWithTitle:@"Done"
                    style:UIBarButtonItemStyleDone
                    target:yourtextfield 
                    action:@selector(resignFirstResponder)],nil ] animated:YES];

但是,如果您更喜欢使用操作表,那么这是基本设置:

在操作表中添加 UIPickerView 和按钮 - 如何?

于 2012-08-23T16:41:50.157 回答
2

您可以设置UIDatePickerUITextField输入视图 ( @property inputView)。ActionSheet 只能包含按钮。

于 2012-08-23T16:30:53.937 回答