Passing Data between View Controllers : from uitableview to a details view controller

Discussion in 'iOS Programming' started by EliasRahme, Jul 18, 2012.

  1. EliasRahme macrumors newbie

    Joined:
    Jul 18, 2012
    #1
    Hy all , I am using IOS 5 and Storyboard coding. I have built an application in which i have a tableView connected to a sqlite database with a search bar. When we touch a row, it will take us automatically to another view controller called "Details". I need to pass data from my table view to the details view controller, pass for example the author.title to the labelText.text field. Any ideas?
     
  2. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #2
    When you create a core data based iOS project in xcode, it has example code that shows how to do this.

    To do this, your detailViewController needs to have a property that holds the item you're displaying, like @property id detailItem In your tableviewcontroller in the prepareForSegue method you would assign the selected row item to the detailItem in the detailViewController

    If you plan to edit the data in the detailViewController, then you would also need a managedObjectContext property and pass that in as well.
     
  3. EliasRahme thread starter macrumors newbie

    Joined:
    Jul 18, 2012
    #3
    I did try that but it doesn't work ... here's a snippet of my code :
    In the main tableViewController :


    Code:
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        Details *dv = (Details*)segue.destinationViewController;
        dv.labelText.text = author.title;
    }
    
     
  4. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #4
    You can't (and shouldn't) change the value of another view controllers views in prepareForSegue. (At that point, the other view controller has not created it's views.) It is also very bad practice from a software design point of view.

    As the other poster said, you need to create a data property in your details view controller and set that. Then, in your details view controllers viewWillAppear method, take the data from that property and install it in the label (if that's what the view controller needs to do with the data.)
     
  5. EliasRahme thread starter macrumors newbie

    Joined:
    Jul 18, 2012
    #5
    can someone post me a code snippet or something? my whole application is finished when i get through this part please
     
  6. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #6
    Did you try what I said? I told you what to do.
     
  7. EliasRahme thread starter macrumors newbie

    Joined:
    Jul 18, 2012
    #7
    Yes i did, if i try to assign a static value to the LabelText.text , it works and i see changes but if i assign it to the variable , as @Duncan you suggested, it doesn't work and no output is shown, that's why I'm asking for a code snippet .. maybe i am doing anything wrong..thank you for you time
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Show us what you tried, based on Duncan's advice.
     

Share This Page