【问题标题】:Adding UIBarButtonItem to UIActionSheet navigation bar (UIDatePicker)将 UIBarButtonItem 添加到 UIActionSheet 导航栏 (UIDatePicker)
【发布时间】:2014-01-31 18:23:18
【问题描述】:

我创建了 UIActionSheet 并在其上添加了一个 UIPickerView。现在,我想在 UIActionSheet 导航栏标题的右侧和左侧添加两个按钮。我该怎么做?

这是我到目前为止所做的:

- (IBAction)userDidClickedOnSelectDate:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select your birthday date:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select", nil];
    [actionSheet showInView:self.view];
    [actionSheet setFrame:CGRectMake(0, 100, 570, 383)];
}

我正在尝试以这种方式添加 UIButton,但我无法弄清楚为什么我在视图中看不到它。当我检查 UIActionSheet 子视图时,他就在那里(可能在下面的某个地方)。

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, 50, 50, 216)];

    //  Configure picker...
    [pickerView setDatePickerMode:UIDatePickerModeDate];
    [pickerView setTag:ACTION_SHEET_PICKER_TAG];

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

    //  Add button to the action sheet
    UIButton *buttonCancel = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonCancel setFrame:CGRectMake(5, 100, 200, 200)];
    [buttonCancel setTitle:@"close" forState:UIControlStateNormal];
    [buttonCancel setBackgroundColor:[UIColor redColor]];
    [actionSheet addSubview:buttonCancel];

    //  Gets an array af all of the subviews of our actionSheet
    NSArray *subviews = [actionSheet subviews];
    for (id button in subviews) {
        if ([button isKindOfClass:[UIButton class]]) {
            [button setUserInteractionEnabled:NO];
            [button setHidden:YES];
        }
    }
}

看起来是这样的:

我发现有一种方法可以到达 UIActionSheet 导航栏或工具栏(标题所在的位置)并在顶部添加按钮。

提前致谢。


根据人们的回答,我设法创建了一个更好的选择器视图,该视图呈现相同,但处理和构建方式要好得多。我创建了一个 UIViewController 并添加了一个 UIDatePicker 和一个顶部带有按钮的 UIToolBar。比这样称呼它:

- (IBAction)userDidClickedOnSelectDate:(id)sender
{
    CustomDatePicker *customPicker = [[CustomDatePicker alloc] initWithNibName:@"CustomDatePicker" bundle:nil];
    customPicker.delegate = self;
    customPicker.view.backgroundColor = [UIColor clearColor];
    self.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:customPicker animated:YES completion:nil];
}

我还创建了一个代表,告诉我何时单击了取消和完成按钮:

- (void)datePickerDidCancelled:(CustomDatePicker *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)datePicker:(CustomDatePicker *)picker dateDidSelected:(NSDate *)date
{
    NSLog(@"selectedDate: %@", date);
    [self dismissViewControllerAnimated:YES completion:nil];
}

【问题讨论】:

  • 您确定要为此使用操作表吗?听起来更像是模态视图更好...
  • 这似乎是一种非常肮脏的技术,有点像在UIAlertView 对象上执行addSubview。我怀疑苹果会接受这个。

标签: ios objective-c uibutton uipickerview uiactionsheet


【解决方案1】:

根据documentation,您不应将子视图添加到 UIActionSheets。

UIActionSheet 不是为子类而设计的,也不应该添加 对其层次结构的看法。如果您需要展示一张包含更多内容的表格 自定义比 UIActionSheet API 提供的,您可以创建 您自己的并以模态方式呈现 presentViewController:animated:completion:.

【讨论】:

    【解决方案2】:

    我通常处理日期输入的方式是为控件制作自定义输入视图(例如,UILabelUITextField)。我创建了自己的子类来处理这个问题,但是您可以非常简单地在任何地方临时进行。

    1. 将控件的InputView 属性设置为您的pickerView
    2. 将控件的InputAccessoryView 属性设置为您创建的UIToolbarUINavigationBar
    3. 将自定义按钮添加到工具栏/导航栏

    如果您要将其添加到 UILabel,请确保它可以成为第一响应者(有关说明,请参阅 this link)。

    如果你想变得花哨,你也可以将你的pickerview嵌入到UIInputView中,这会给你在iOS中每个键盘输入的模糊效果。这部分可能需要一些调整,但最终的效果非常整洁。

    【讨论】: