SimpleTableViewController

SimpleTableViewController

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

问题描述

我的应用程序需要在操作表中添加以下内容.

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

  • 界面工具栏
  • 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 的内容,因为它会导致 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 的导航控制器提供SimpleTableVC"的故事板 ID.
  4. 在 SimpleTableViewController.h 中,创建一个 NSArray 属性来表示表中的数据.
  5. 同样在 SimpleTableViewController.h 中,创建一个协议 SimpleTableViewControllerDelegate,其中包含一个必需的方法 itemSelectedatRow:,以及一个名为 id.这就是我们将选择传递回父控制器的方式.
  6. 在SimpleTableViewController.m中,实现tableview数据源和委托方法,调用tableView:didSelectRowAtIndexPath:中的itemSelectedatRow:.
  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;操作表中的按钮 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 03:50