【发布时间】:2012-06-22 04:25:30
【问题描述】:
我遇到了一个问题,即在 Ipad 中显示带有完成按钮的 UIPickerView。 我通过许多链接和博客进行了详细研究,并得到了“从 UIActionSheet 显示 UIPickerView”的建议
我看到很多与此相关的帖子,但是没有好的答案。所以请不要将其作为重复关闭。
我还能够获得一些好的代码来执行此操作,并且它在我的 Iphone 设备上运行良好。 但是,我发现 Ipad 设备有困难。 操作表未显示为完整视图。 请看下面的截图。这就是结果!!!
用于执行此操作的代码粘贴在下面。
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
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];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
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:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
然后我通过sample pickers从github下载了一个优秀的示例应用程序
下载后,我已将仅对我来说必须的课程复制到我的应用程序中。
他们用来通过 Action-Sheet 显示 UIPickerView+Done 按钮的方法如下所述
ActionStringDoneBlock done = ^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
if ([myLabel respondsToSelector:@selector(setText:)]) {
[myLabel performSelector:@selector(setText:) withObject:selectedValue];
}
};
ActionStringCancelBlock cancel = ^(ActionSheetStringPicker *picker) {
NSLog(@"Block Picker Canceled");
};
NSArray *colors = [NSArray arrayWithObjects:@"Red", @"Green", @"Blue", @"Orange", nil];//picker items to select
[ActionSheetStringPicker showPickerWithTitle:@"Select a Block" rows:colors initialSelection:0 doneBlock:done cancelBlock:cancel origin:myButton];
在最后一行代码中,他们使用参数作为 origin:,我们可以将任何对象(按钮、标签等)传递给它。
Action-sheet 将以原点作为传递的对象。
我的问题又来了:)。我已根据自己的条件使用分段控制来选择时间。
如果我将 mySegment 作为原点参数,Action-sheet 原点箭头将从我的段控件中间显示。而不是从选定的选项卡中显示,这太糟糕了,会给我有价值的用户带来困惑。
所以我在段部分下添加了单独的标签,并为上述方法的 origin 参数提供了它,我解决了我的问题。
但是我知道这不是一个好的解决方法:)
我想知道有什么简单的方法吗?
Apple 是否支持 Ipad 中的 ActionSheet+UIPickerView+DoneButton?
感谢您对此问题的任何帮助
【问题讨论】:
-
查看我的答案,它在我的应用中运行
-
iphone 仅支持 uiactiosheet 。而ipad只支持uipopovercontroller。
标签: objective-c iphone ipad uipickerview actionsheetpicker