0

Currently I have the application loading up the data in a tableview presented to the user that I want them to select the object they wish to view further details about. From their I want to be able to read out the data in the object to the user in various forms either labels or a tableview dependent on the data.

I have been reading around and trying to understand this stuff but it is all still new to me! I had another guy on here help me out with saving the data and passing the data around to store but reading it out is a seemingly different thing. Any examples in swift language would lead me down the right path I know that I need to use the didselectrow method and call a segue and also that I need a prepare for segue but I am not quite sure how it should look.

I have read multiple posts and some do actually attempt to pass objects but not in the manner in which I am trying to..Are you able to pass whole objects after they have been have been selected in a tableview to another view controller to present all data related to that object or are you only able to pass information from that object to the next viewcontroller? I have examples of prepareforsegue and perform segue before not sure what else I am missing but I am currently not able to pass any information between the tableview and the viewcontroller.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "decktoRecord") {

        var detailVC: StatWidgetViewController = segue.destinationViewController as! StatWidgetViewController

        detailVC.deckRecord = decktoPass

    }

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let indexPath = tableView.indexPathForSelectedRow()

    let selectedCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell?

    self.performSegueWithIdentifier("decktoRecord", sender: indexPath);

}

decktoPass is just a variable that contains the type of data entity that is the primary entity of the object.

11
  • Can you add some detail about how you are using deckRecord in StatWidgetViewController (some code from that class would be extremely helpful)? What are you expecting to see and what are you actually seeing? I'd also suggest setting a breakpoint in prepareforSegue to make sure you're actually getting into the if statement. Commented Aug 19, 2015 at 14:53
  • deckRecord is supposed to just be a variable that allows me to pass the object from the tableview to the next viewcontroller..I feel like I am going about this all wrong..and can you give me an example of how to implement the breakpoint sorry I am still really new to all this! Commented Aug 19, 2015 at 15:17
  • Apple has some good documentation on using the debugger: developer.apple.com/library/ios/recipes/… As for your approach, you're on the right track! From what I can see, deckRecord should be populated correctly, but your problem might be in how you're using it in StatWidgetViewController, are you using it to set any labels or such? If so, in which method? Commented Aug 19, 2015 at 15:23
  • I am using it to set a label for the entity attribute deckname which is in the entity Deck. I am also going to want to present to the user the whole of the selections they have made under the deckname that they select from the tableview. Deck name is an object with a relationship to a type, and to a series of options that they select from a different core data entity that then saves those options in yet another entity directly related to the deckname. I hope that makes sense.....sorry really tired today. So basically it will be presented to them as labels and the selections in a tableview. Commented Aug 19, 2015 at 18:16
  • Can you add the code where you set the label to your question? Including the method in which it is called. No rush, can understand having one of those days, but I think it will give us a better idea of what's going wrong. Commented Aug 19, 2015 at 18:18

1 Answer 1

2

A little confused by what you're asking but from what I understand, once the user clicks on a cell you want to segue to another view controller where they can edit the the details?

To add an exception breakpoint, open up your left panel with all your files/viewcontrollers, at the very top of it should be a small panel with a few icons, the first one is a folder, click on the second last one (the one that looks like a tag) click on the plus in the bottom right and click add exception breakpoint, this should let you know where the problem in your code is occurring

Okay to edit the details in another View controller start by preparing the segue from the original view controller

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "showTaskDetail" {
            let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController
            let indexPath = self.tableView.indexPathForSelectedRow()
            let thisTask = fetchedResultsController.objectAtIndexPath(indexPath!) as! TaskModel
            detailVC.detailTaskModel = thisTask
            detailVC.delegate = self

        }
        else if segue.identifier == "showTaskAdd" {
            let addTaskVC:AddTaskViewController = segue.destinationViewController as! AddTaskViewController
            addTaskVC.delegate = self
        }
    }

okay so like the code is showing above, I have a segue called showTaskDetail which shows the details of whatever it is, in my case its a simple task. You said you want to edit this information in another view controller when a user clicks on the row, well you need to be able to get this information in the other view controller.

So create a variable in the other viewcontroller that will hold these values, for me i called it detailTaskModel

 var detailTaskModel: TaskModel!

Incase you're confused about what TaskModel is, I'm using CoreData to store my data, and TaskModel is a NSMangedObject class.

let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController

this line you're just specifying what your other view controller is, replace TaskDetailViewController with your swift class.

let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController

this line fetches the data from the row selected

Now you should be able to pass your information into the other view controller and edit it using the detailTaskModel, do you need help with that as well?

Sign up to request clarification or add additional context in comments.

10 Comments

Yea exactly to the first part and as far as the second part lol I had a mind blank I forgot about breakpoints normally I am accidentally clicking those on lol! Thanks for the answer I was so exhausted yesterday I wasn't able to think clearly....
I edited my answer above to include what you need to do, let me know if you need any more help
Ok thanks I think I see your answer has a massive difference on passing data vs what I programmed in..specifically in your prepare to segue I understood what I need to do but the problem for me always comes in this line of code let indexPath = self.tableView.indexPathForSelectedRow() I tried using this multiple times but I keep getting the same message "Cannot invoke 'indexPathForSelectRow' with no arguments"
I am unsure why when I try to use that segment of code it doesn't let me within the prepareforsegue I can only do it in the didselectatrow method....which doesn't help me at all
Got it never mind :) Thank you for your help with this after I sat back and looked at the way I was presenting the data I realized that I was setting self for the tableview as though it was a tableview controller. I was just presenting the tableview in a regular view controller. The code ended up looking like the comment below.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.