Reputation: 23
was hoping to get some help with identifying why my prepareForSegue() is not working.
Here is my Main.storyboard and the identifier for the segue. https://i.sstatic.net/GFsqF.png
I am linking the Prototype cell as a selection segue (show) to a UITabBarController which will have a few UIViewController connect to it. I have placed breakpoints in the prepareForSegue and see that it never goes into the function, I have a suspicion that I may have setup the segue incorrectly but my inexperience in iOS development is causing me to overlook what the problem is.
Here is my prepareForSegue on my prepareForSegue() on my MainViewController
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showLesson" {
var tabBarVC: UITabBarController = segue.destinationViewController as UITabBarController
var descVC: LessonsDetailsViewController = tabBarVC.viewControllers?.first as LessonsDetailsViewController
var row = tableView!.indexPathForSelectedRow()!.row
var dataObj = lessonsNSMObj[row]
descVC.managedObject = dataObj
}
}
I did find a work around but I am not sure if that is the appropriate way of doing this or if there is downfall of doing it this way.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showLesson" {
var tabBarVC: UITabBarController = segue.destinationViewController as UITabBarController
var descVC: LessonsDetailsViewController = tabBarVC.viewControllers?.first as LessonsDetailsViewController
var row = sender as Int
var dataObj = lessonsNSMObj[row]
descVC.managedObject = dataObj
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
performSegueWithIdentifier("showLesson", sender: row)
}
Any help would be greatly appreciative! I also have the whole project available on github: https://github.com/schuob/
Upvotes: 2
Views: 7945
Reputation: 104092
Ok, after looking at your code, I found the problem. You're registering the class in viewDidLoad, and you shouldn't do that when you have a cell in the storyboard that you hooked up to a segue. Registering the class, tells the table view to get its cells from the class's code, not from IB, so the cell in IB that you originate the segue from is not the one you're dequeueing. To fix the problem, just delete that line where you register the class. You should also delete the performSegue line in didSelectRowAtIndexPath, or you'll be trying to segue twice.
You need to change your prepareForSegue to get the indexPath from the cell. The cell will be the sender, so you can do it like so,
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let cell = sender as UITableViewCell
if segue.identifier == "showLesson" {
var tabBarVC: UITabBarController = segue.destinationViewController as UITabBarController
var descVC: LessonsDetailsViewController = tabBarVC.viewControllers?.first as LessonsDetailsViewController
var row = self.tableView.indexPathForCell(cell)?.row
var dataObj = lessonsNSMObj[row!]
descVC.managedObject = dataObj
}
}
You also need to change the identifier in cellForRowAtIndexPath to "LessonCell" (with a capital "L") since that's what you called it in the storyboard.
Upvotes: 1
Reputation: 2938
There is nothing wrong with your use of prepareForSegue or performSegueWithIdentifier. However you appear to have a bug in your ManagedObject code. I took your code and ran it and was getting errors in the Managed Object access (array index out of range). I commented out the buggy statements and executed the code. The prepareForSegue as well as the performSegueWithIdentifier both executed as you intended.
I beg to differ in opinion with the comments of rdelmar and '1s and 0s' concerning segue-ing from rows. It is routinely done in several Apple programs I have seen. I don't see anything wrong.
Upvotes: 0