TableViewController 未接收到触摸 [英] TableViewController not receiving touches

查看:25
本文介绍了TableViewController 未接收到触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意: 我最初问这个问题想知道为什么 didSelectRowAtIndex 没有被调用.进一步深入研究,我意识到我的核心问题实际上是我的一个视图没有接收到触摸事件.我将把这个问题留给后人,并提出一个更明确的版本.如果管理员想酌情关闭或删除此问题,请继续操作.

NOTE: I originally asked this question wondering why didSelectRowAtIndex was not getting called. Digging into it further, I've realized that my core problem is really with one of my views not receiving touch events. I am going to leave this question here for posterity and ask a more clarified version of it. If an admin would like to close or delete this question as appropriate, please go ahead and do so.

我正在我的应用程序中实现一个侧边栏(如汉堡包菜单),并且我正在使用 UITableViewController 来完成它.我可以显示侧边栏并且单元格正在正确初始化.

I am implementing a sidebar in my app (like a hamburger menu) and I'm using a UITableViewController to do it. I can get the sidebar to show up and cells are initializing correctly.

我的代码非常简单,我在这里包含了所有内容.现在我只有一个用于主屏幕的视图控制器和一个用于侧边栏的表视图控制器.任何帮助表示赞赏!

My code is pretty simple, I am including all of it here. Right now I just have a view controller for the main screen and a table view controller for the sidebar. Any help is appreciated!

更新:看起来问题不在于没有调用 didSelectRowAtIndexPath——而是我的 SimpleTableViewController 根本没有收到任何触摸!我尝试覆盖 'touchesBegan:withEvent:' 在 ViewController 和 SimpleTableViewController 中:ViewController 接收触摸很好,但表视图控制器似乎什么也没收到.

Update: it looks like the problem is not that didSelectRowAtIndexPath is not being called -- it's that my SimpleTableViewController is not receiving any touches at all! I tried overriding 'touchesBegan:withEvent:' in both the ViewController and the SimpleTableViewController: the ViewController receives touches just fine, but the table view controller appears to receive nothing.

进一步更新:我意识到问题与我如何制作动画以在屏幕右侧显示表格视图有关.现在我将主应用程序视图推"到左侧,这将拉"容器视图.当我这样做时,当我点击 containerView 时,不会对其进行任何触摸.

Further update: I've realized that the issue has to do with how I am doing the animation to reveal the tableview on the right hand side of the screen. Right now I am "pushing" the main application view to the left, which "pulls" the containerView along with it. When I do that, no touches go to the containerView when I tap on it.

但是!如果我拉"containerView 在主视图的顶部,可以很好地接收触摸.也许我在这里遗漏了一些基本的东西,关于 iOS 认为触摸是合法的屏幕的活动"部分?

However! If I "pull" the containerView on top of the main view, touches are received just fine. Maybe I am missing something elementary here regarding what iOS considers to be the "active" part of the screen where touches are legal?

代码在这里:

之前 -- BROKEN

@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,显示了当触摸不起作用时应用程序的样子.

Here is a gif showing what the app looks like when touches don't work.

之后——工作

@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,显示了当触摸起作用时应用程序的样子.我添加了一些代码来更改主视图的背景颜色,以说明正在接收触摸.

Here is a gif showing what the app looks like when touches do work. I added some code to change the background color of the main view to illustrate that touches are being received.

代码如下 - 我之前在这里有我的整个实现,但我已经将其编辑为仅包含相关部分.

Code follows - I previously had my entire implementation here but I've redacted it to include only the relevant parts.

首先是主视图控制器:

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

For what it's worth, if you'd like to see the whole app (it's just a learning app) you can go here: https://github.com/bitops/sagacious-quack

推荐答案

当您将另一个视图控制器的视图添加到您的视图层次结构时,您必须让父级和子级都知道,以便父级可以将相关消息转发给子级.这在 Apple 的 视图控制器编程指南,在实现自定义容器视图控制器"部分.

When you add another view controller's view to your view hierarchy, you have to let both parent and child know, so the parent can forward relevant messages to the child. This is explained in Apple's View Controller Programming Guide, in the "Implementing a Custom Container View Controller" section.

就你而言,你需要这样的东西:

In your case, you need something like this:

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

这篇关于TableViewController 未接收到触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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