添加UIPickerView&行动表中的按钮 - 如何? [英] Add UIPickerView & a Button in Action sheet - How?

查看:81
本文介绍了添加UIPickerView&行动表中的按钮 - 如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的申请需要在操作表中添加以下内容。

My application requires following things to be added in an action sheet.


  • UIToolbar

  • UIToolbar上的按钮

  • UIPicker控件

我已经包含了一张图片来了解我的要求。

I have included an image to understand my requirements.

您能否解释一下,如何实施?

Could you please explain, how this can be implemented?

推荐答案

iOS 7更新

UIActionSheet的Apple文档 UIActionSheet不是设计为子类,也不应该添加其层次结构的视图

我建议不要尝试自定义ActionSheet的内容,因为它可能会导致严重的无效上下文e iOS 7中的错误。我只花了几个小时解决这个问题,最终决定采取不同的方法。我用一个包含简单tableview的模态视图控制器替换了调用以显示操作表。

I recommend against trying to customize the contents of an ActionSheet, as it can lead to serious invalid context errors in iOS 7. I just spent a few hours working through this problem and ultimately decided to take a different approach. I replaced the call to show the action sheet with a modal view controller containing a simple tableview.

有很多方法可以实现这一目标。这是我刚刚在当前项目中实现的一种方式。这很好,因为我可以在5到6个不同的屏幕之间重复使用它,我可以从选项列表中选择所有用户。

There are many ways to accomplish this. Here's one way that I just implemented in a current project. It's nice because I can reuse it between 5 or 6 different screens where I all users to select from a list of options.


  1. 创建一个新的UITableViewController子类, SimpleTableViewController

  2. 在故事板中创建一个UITableViewController(嵌入在导航控制器中)并将其自定义类设置为SimpleTableViewController。

  3. 为SimpleTableViewController的导航控制器提供一个Storyboard ID为SimpleTableVC。

  4. 在SimpleTableViewController.h中,创建一个NSArray属性来表示表中的数据。

  5. 同样在SimpleTableViewController.h中,创建一个协议 SimpleTableViewControllerDelegate 带有必需的方法 itemSelectedatRow:,以及一个名为委托的弱属性 id< SimpleTableViewControllerDelegate> ; 。这就是我们将选择传递回父控制器的方法。

  6. 在SimpleTableViewController.m中,实现tableview数据源和委托方法,调用 itemSelectedatRow: in tableView:didSelectRowAtIndexPath:

  1. Create a new UITableViewController subclass, SimpleTableViewController.
  2. Create a UITableViewController in your storyboard (embedded in a navigation controller) and set its custom class to SimpleTableViewController.
  3. Give the navigation controller for SimpleTableViewController a Storyboard ID of "SimpleTableVC".
  4. In SimpleTableViewController.h, create an NSArray property that will represent the data in the table.
  5. Also in SimpleTableViewController.h, create a protocol SimpleTableViewControllerDelegate with a required method itemSelectedatRow:, and a weak property called delegate of type id<SimpleTableViewControllerDelegate>. This is how we will pass the selection back to the parent controller.
  6. In SimpleTableViewController.m, implement the tableview data source and delegate methods, calling itemSelectedatRow: in tableView:didSelectRowAtIndexPath:.

这种方法有相当可重用的额外好处。要使用,请在ViewController.h中导入SimpleTableViewController类,符合SimpleTableViewDelegate,并实现 itemSelectedAtRow:方法。然后,打开模态只是实例化一个新的SimpleTableViewController,设置表数据和委托,然后呈现它。

This approach has the added benefit of being fairly reusable. To use, import the SimpleTableViewController class in your ViewController.h, conform to the SimpleTableViewDelegate, and implement the itemSelectedAtRow: method. Then, to open the modal just instantiate a new SimpleTableViewController, set the table data and delegate, and present it.

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

我创建了一个简单的例子,将其发布在github上

I create a simple example and posted it on github.

另见显示动作表导致CGContext无效的上下文错误

这篇关于添加UIPickerView&amp;行动表中的按钮 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆