I have a UITableViewController
populated with NSManagedObjects
from an NSFetchRequest
called in viewDidLoad
.
Everything works as desired on the myTableViewController
with the exception of accessoryButtonTappedForRowWithIndexPath
, which returns indexPath
0.
Here's how I'm implementing accessoryButtonTappedForRowWithIndexPath
:
override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("reorderSequences", sender: UIButton())
}
Here's my 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)
}
}
}
My understanding is using the UIButton
as the sender lets me figure out which indexPath
the sender lives in performSegueWithIdentifier
. Obviously, that's not the case. What am I missing?
UPDATE
For every easy question, there's a easy answer:
In prepareForSegue
I made indexPath
the sender
, like so:
override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("reorderThings", sender: indexPath)
}
In prepareForSegue
, I got a hold of the indexPath that was passed like so:
let indexPath = sender as! NSIndexPath
If you're running a bunch of segues, you want to be careful. For my purposes, it works. Thank you to Tom Harrington for pointing me in the right direction.
In this line:
performSegueWithIdentifier("reorderSequences", sender: UIButton())
You are not sending "the" UIButton
. You're creating a brand-new instance of UIButton
. It's not the one you tapped. It's not on the screen or in the view hierarchy anywhere. It has default values for everything. So when you get to performing the segue, there's no useful information in the incoming UIButton
, and you get the same result every time.