TableViewController没有接收到触摸

注意:我最初问这个问题,为什么没有调用SelectSelectRowAtIndex。 进一步深入研究,我意识到我的核心问题是我的观点之一是没有接受触摸事件。 我将在这里留下这个问题的后代,并要求一个更清晰的版本。 如果pipe理员想要closures或删除此问题,请继续操作。

我正在我的应用程序中实现一个侧栏(就像一个汉堡菜单),我正在使用UITableViewController来做到这一点。 我可以让边栏显示,单元格正确初始化。

我的代码非常简单,我在这里包括所有的代码。 现在我只有主屏幕的视图控制器和边栏的表视图控制器。 任何帮助表示赞赏!

更新:它看起来像问题不是didSelectRowAtIndexPath没有被调用 – 这是我的SimpleTableViewController没有收到任何接触! 我试着在ViewController和SimpleTableViewController中重写'touchesBegan:withEvent:':ViewController接收触摸就好了,但是表视图控制器似乎什么也没有收到。

进一步更新:我已经意识到,这个问题与我如何做animation来显示屏幕右侧的tableview。 现在我正在将主应用程序视图“推”到左侧,将“视图”一起“拉”到一起。 当我这样做的时候,当我点击它的时候没有碰到containerView。

然而! 如果我“拉”在主视图顶部的containerView,触摸收到就好了。 也许我错过了一些关于iOS认为是触摸合法的屏幕的“活动”部分的基本知识吗?

代码在这里:

之前 – 损坏

@IBAction func push() { // containerView is "pulled" alongside self.view UIView.animateWithDuration(3.0) { self.view.frame = CGRectMake(self.originalX - 250, self.originalY, self.view.frame.width, self.view.frame.height) } } 

这里是一个显示什么应用程序看起来像什么时候触摸不起作用的gif。

notouchesapp

之后 – 作品

 @IBAction func push() { // containerView is "pulled" on top of self.view UIView.animateWithDuration(3.0) { self.containerView.frame = CGRectMake(self.originalX - 250, self.originalY, 250, self.frameHeight) } } 

这是一个GIF显示应用程序看起来像什么时候触摸工作。 我添加了一些代码来改变主视图的背景颜色,以说明触摸正在被接收。

在这里输入图像说明

代码如下 – 我之前已经完成了我的整个实现,但是我已经编写了它,只包含相关的部分。

首先是主视图控制器:

 import UIKit class ViewController: UIViewController, SimpleTableViewControllerDelegate { var x = UIView() var y = UIView() var containerView = UIView() let animationDuration = 1.5; let delayTime = 0.25; let animationTime = 0.25; var tableViewController = SimpleTableViewController() override func viewDidLoad() { super.viewDidLoad() originalX = self.view.frame.origin.x originalY = self.view.frame.origin.y frameHeight = self.view.frame.height frameWidth = self.view.frame.width containerView.frame = CGRectMake(frameWidth, originalY, 250, frameHeight) containerView.backgroundColor = UIColor.magentaColor() containerView.clipsToBounds = false view.addSubview(containerView) tableViewController.items = ["Lemon", "Lime", "Agave"] tableViewController.delegate = self tableViewController.tableView.dataSource = tableViewController tableViewController.tableView.delegate = tableViewController tableViewController.tableView.frame = containerView.bounds tableViewController.tableView.backgroundColor = UIColor.lightGrayColor() tableViewController.tableView.scrollsToTop = false tableViewController.tableView.separatorStyle = .None tableViewController.tableView.contentInset = UIEdgeInsets(top: 64.0, left: 0, bottom: 0, right: 0) tableViewController.tableView.reloadData() addChildViewController(tableViewController) containerView.addSubview(tableViewController.tableView) tableViewController.didMoveToParentViewController(self) } func didSelectRowAtIndexPath(indexPath: NSIndexPath) { NSLog("[ViewController] invoked didSelectRowAtIndexPath with \(indexPath.row)") } var originalX : CGFloat! var originalY : CGFloat! var frameHeight : CGFloat! var frameWidth : CGFloat! @IBAction func push() { UIView.animateWithDuration(animationTime, delay: 0, options: .CurveLinear, animations: { self.view.frame = CGRectMake(self.originalX - 250, self.originalY, self.view.frame.width, self.view.frame.height) }, completion: { (var b) -> Void in }) } @IBAction func pull() { UIView.animateWithDuration(animationTime, delay: 0, options: .CurveLinear, animations: { self.view.frame = CGRectMake(self.originalX, self.originalY, self.view.frame.width, self.view.frame.height) }, completion: { (var b) -> Void in }) } } 

对于它的价值,如果你想看到整个应用程序(这只是一个学习应用程序),你可以去这里: https : //github.com/bitops/sagacious-quack

当您将另一个视图控制器的视图添加到视图层次结构中时,必须让父项和子项都知道,因此父项可以将相关消息转发给子项。 这在Apple的“ 视图控制器编程指南 ”的“实现自定义容器视图控制器”一节中进行了解释。

在你的情况下,你需要这样的东西:

 [self addChildViewController:tableViewController]; containerView.addSubview(tableViewController.tableView) [tableViewController didMoveToParentViewController:self]; 

在ViewController的viewDidLoad()中:

  //Add these two lines tableViewController.tableView.dataSource = tableViewController tableViewController.tableView.delegate = tableViewController tableViewController.delegate = self tableViewController.tableView.frame = containerView.bounds tableViewController.tableView.backgroundColor = UIColor.lightGrayColor() tableViewController.tableView.scrollsToTop = false tableViewController.tableView.separatorStyle = .None tableViewController.tableView.contentInset = UIEdgeInsets(top: 64.0, left: 0, bottom: 0, right: 0) tableViewController.tableView.reloadData()