Reputation: 6679
I'm new to swift and what I'm trying to figure out is how to make a detail view controller the segues from a table view. Right now I have the segue (with "showDetails" identifier) set up to open a the detail view controller. Here's the code I have in the table view controller that I want to segue from, just not sure if I'm doing this right. Any help would be appreciated!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
//Core Data
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let managedContext : NSManagedObjectContext = appDelegate.managedObjectContext!
var fetchRequest = NSFetchRequest(entityName: "Log")
fetchRequest.returnsObjectsAsFaults = false;
var results: NSArray = managedContext.executeFetchRequest(fetchRequest, error: nil)!
var totalHoursWorkedSum: Double = 0
var logsArray = [String]()
for res in results {
var totalWorkTimeInHours = res.valueForKey("totalWorkTimeInHours") as Double
var dateString = res.valueForKey("dateString") as String
var comments = res.valueForKey("comments") as String
var loggedTotalWorkTimeInHours = "\(totalWorkTimeInHours)"
var loggedDateString = "\(dateString)"
var loggedComments = "\(comments)"
let destinationVC = segue.destinationViewController as LogDetailTableViewController
destinationVC.logTimeTextField.text = loggedTotalWorkTimeInHours
destinationVC.logDateTextField.text = loggedDateString
destinationVC.commentsTextField.text = loggedComments
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedLog: AnyObject = loggedTimes[indexPath.row]
let destinationVC = LogDetailTableViewController()
destinationVC.logTimeTextField = selectedLog as UITextField
destinationVC.logDateTextField = selectedLog as UITextField
destinationVC.commentsTextField = selectedLog as UITextField
destinationVC.performSegueWithIdentifier("showDetails", sender: self)
}
Not sure what to do differently I keep getting a nil crash when I try to tap on the tableview cell to try to show detail.
Upvotes: 0
Views: 154
Reputation: 9352
There are a couple issues with your code. First, with segues, you don't create the destination view controller - it's created for you. You should remove the line in didSelectRowAtIndexPath that creates your detail view controller because that won't be segued to.
Second, depending on how you wire up the segue from the table view controller, you may or may not need to call performSegueWithIdentifier.
A common way to wire up a segue in the storyboard is from a prototype cell to the detail view controller. With this approach, the segue is automatically triggered when user taps the cell. You don't need to call performSegueWithIdentifier explicitly.
On the other hand, if you wired up the segue from the table view controller to the detail view controller, then you do need to call it.
In either case, you will need to get the relevant info from the selected cell to pass along to the detail view controller in prepareForSegue. One technique I use is to save a reference to the cell data (your selectedTime) in a class level property, and then send that to the destination view controller by means of a property I define on it.
Upvotes: 1