我的应用程序需要在操作表中添加以下内容。
UIToolbar
上的
我已经添加了一张图像来理解我的需求。
你能解释一下,这是如何实现的吗?
发布于 2013-10-02 11:09:52
iOS 7的更新
Apple docs for UIActionSheet:UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy
我建议不要尝试自定义ActionSheet的内容,因为这可能会导致iOS 7中出现严重的无效上下文错误。我只是花了几个小时来解决这个问题,最终决定采取一种不同的方法。我将显示操作表的调用替换为包含一个简单的表视图的模态视图控制器。
有很多方法可以做到这一点。这是我刚刚在当前项目中实现的一种方法。这很好,因为我可以在5到6个不同的屏幕上重用它,所有用户都可以从选项列表中进行选择。
创建一个新的storyboard子类,在您的故事板(嵌入在导航控制器中)中创建一个故事板,并将其自定义类设置为SimpleTableViewController.
SimpleTableViewControllerDelegate
,其中包含一个必需的方法itemSelectedatRow:
,以及一个名为id<SimpleTableViewControllerDelegate>
类型的称为delegate的弱属性。这就是我们如何将选择传递回父controller.tableView:didSelectRowAtIndexPath:
.中调用itemSelectedatRow:
这种方法还有一个额外的好处,那就是相当可重用。要使用,请在ViewController.h中导入SimpleTableViewController类,符合SimpleTableViewDelegate,并实现itemSelectedAtRow:
方法。然后,要打开该模式,只需实例化一个新的SimpleTableViewController,设置表数据和委托,并呈现它。
UINavigationController *navigationController = (UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:@"SimpleTableVC"];
SimpleTableViewController *tableViewController = (SimpleTableViewController *)[[navigationController viewControllers] objectAtIndex:0];
tableViewController.tableData = self.statesArray;
tableViewController.navigationItem.title = @"States";
tableViewController.delegate = self;
[self presentViewController:navigationController animated:YES completion:nil];
我创建了一个简单的示例和posted it on github。
另请参阅Showing actionsheet causes CGContext invalid context errors。
发布于 2010-01-16 04:08:26
另一个解决方案:
UIActionSheet *actionSheet = [UIActionSheet alloc :nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];setActionSheetStyle:UIActionSheetStyleBlackTranslucent;actionSheet CGRect pickerFrame = CGRectMake(0,40,0,0);UIPickerView *UIPickerView=[ pickerView.showsSelectionIndicator alloc :pickerFrame];pickerView.showsSelectionIndicator= YES;pickerView.dataSource = self;pickerView.delegate = self;actionSheet addSubview: pickerView;pickerView release;UISegmentedControl *closeButton = [UISegmentedControl alloc :NSArray arrayWithObject:@"Close"];closeButton.momentary = YES;closeButton.frame = CGRectMake(260,7.0f,50.0f,30.0f);closeButton.segmentedControlStyle = UISegmentedControlStyleBar;=;closeButton addTarget:自身操作:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged;actionSheet addSubview: closeButton;closeButton release;[actionSheet showInView:[UIApplication sharedApplication keyWindow]];actionSheet setBounds:CGRectMake(0,0,320,485);
发布于 2011-01-07 11:44:59
尽管这个问题很老了,但我还是会很快提到,我已经将一个带有方便函数的ActionSheetPicker class组合在一起,这样您就可以在一行中生成一个带有UIPickerView的ActionSheet。它基于这个问题的答案中的代码。
编辑:它现在还支持使用DatePicker和DistancePicker。
更新:
此版本已弃用:请改用ActionSheetPicker-3.0。
https://stackoverflow.com/questions/1262574
复制相似问题