0

我有一个来自被调用的UITableViewController填充。NSManagedObjectsNSFetchRequestviewDidLoad

除了返回0myTableViewController之外,一切都在 上按预期工作。accessoryButtonTappedForRowWithIndexPathindexPath

这是我的实施方式accessoryButtonTappedForRowWithIndexPath

override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("reorderSequences", sender: UIButton())
}

这是我的performSegueWithIdentifier

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "reorderSequences" {
            // Get a hold of the item to send to the destinationVC
            let button = sender as! UIButton
            let hitPoint = button.convertPoint(CGPointZero, fromCoordinateSpace: self.tableView)
            if let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(hitPoint) {
                // ** Regardless of which accessoryButton I press, the indexPath 
                // for index 0 is always the value **
                print("\nhitPoint's x: \(hitPoint.x)\n hitPoint's y: \(hitPoint.y)")
                let selectedThingSequence = self.thingSequences[indexPath.row] 
                print("prepareForSegue's indexPath is \(indexPath)")
            // set up the segue
                let destinationVC = segue.destinationViewController as! ReorderTableViewController
                destinationVC.thingSequenceToReorder = selectedThingSequence
                print(controller.description)
            }
        }
    }

我的理解是使用UIButton作为发件人让我弄清楚indexPath发件人住在哪个performSegueWithIdentifier。显然,事实并非如此。我错过了什么?

更新

对于每一个简单的问题,都有一个简单的答案:

prepareForSegue我制作indexPath的 中sender,像这样:

override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
            performSegueWithIdentifier("reorderThings", sender: indexPath)
}

prepareForSegue中,我得到了这样传递的 indexPath:

let indexPath = sender as! NSIndexPath

如果你正在运行一堆 segue,你要小心。就我的目的而言,它有效。感谢 Tom Harrington 为我指明了正确的方向。

4

1 回答 1

1

在这一行:

performSegueWithIdentifier("reorderSequences", sender: UIButton())

您没有发送 "the" UIButton。您正在创建一个全新的UIButton. 这不是你点击的那个。它不在屏幕上或任何地方的视图层次结构中。它具有所有内容的默认值。因此,当您开始执行 segue 时,传入的信息中没有有用的信息UIButton,并且每次都会得到相同的结果。

于 2015-10-19T17:15:55.860 回答