【问题标题】:Display UIDatePicker in UIActionSheet when tapping on UITextField点击 UITextField 时在 UIActionSheet 中显示 UIDatePicker
【发布时间】:2012-08-19 05:36:50
【问题描述】:

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

【问题讨论】:

  • 到目前为止你有没有尝试过?通常在 StackOverflow 上,当您尝试了一些不起作用的东西时,您应该提出问题,而不是要求我们做您必须做的事情;)此外,谷歌搜索会给出答案(顺便说一下,在 StackOverflow 上)。
  • 我在 google bt 上搜索没有得到完美的答案 :( 好的,谢谢 :)

标签: iphone objective-c ios ios4


【解决方案1】:

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

【讨论】:

  • 确实是为什么?它看起来是一个非常优雅的解决方案!
【解决方案2】:

为此,您需要实现 UIActionShetDelegateUITextFieldDelegatetextFieldDidBeginEditing 中,您需要显示操作表,例如:

-(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)];

}

【讨论】:

  • @AmitJarsaniya:很高兴:)
【解决方案3】:

我发现当使用带有选择器的文本字段时,最简单的方法是设置文本字段的输入视图并添加您自己的工具栏以方便使用。确保你有你的“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];

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

Add UIPickerView & a Button in Action sheet - How?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多