【发布时间】: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