Newbie needs help with accessing variables across views/classes

Discussion in 'iOS Programming' started by jackal123uk, Oct 23, 2011.

  1. jackal123uk macrumors member

    Joined:
    Jan 13, 2007
    Location:
    England
    #1
    Hi all,

    Over the past couple of months I've put together my first iOS app and on the whole it's going well. I'll be the first to admit that biggest my downfall is not fully understanding the underlying objective C foundation, and I find Apple's documentation on the subject quite hard going.

    My app in similar to the Contacts app; it reads a SQL database to generate a tableview, where each row drills down to an "details" page which displays data and a map thumbnail taken from the database. The map thumbnail is a small mapview generated with hardcoded location co-ordinates from the database. When the user taps the thumbnail a larger map with interaction enabled is displayed.

    The problem is that no matter how I try I cannot pass the co-ordinate data used to generated the thumbnail on to the main mapview. I've tried to reference the co-ordinates of the detailview class from within the mapview class and tried defining the new co-ordinates of the mapview class from within the detailview class. Methods which have worked before...

    Anyone know of any good tutorials on how to share variables between views/classes? Any help would be greatly appreciated.

    Thanks in advance.
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    When you push the new view (with your big map), you just use the initWithNibName?
    you could try creating your own init method, where you pass an object, which holds all the data, then you can retain the data in your new controller, and use it there :) or save one in a singleton method, but this is not what you're looking for in this case i think.
     
  3. jaikob macrumors 6502

    jaikob

    Joined:
    Jul 1, 2008
    Location:
    Freeland, MI
    #3
    Exactly what he said. Create an initWithYourObject:YourObj method, and just call it: [[YourViewController alloc] initWithYourObject:Object];
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Another way is to create properties for your coordinates in your map view's controller and then set those properties after your instantiate it but before you present it (whether modally or pushed on the nav stack or whatever). Then in the viewDidLoad: you can do what you need to based on the values of those properties. This is a pretty common approach that Apple uses in various places in their sample apps and even in their UITableViewController delegate method for didSelectRowAtIndexPath:.

    Anyways, the overall issue you are dealing with is called data persistence and there are a variety of solutions you can use to achieve it. Search this forum for "sharing values" and/or "data persistence" and you will find a number of other threads already discussing the various solutions.
     
  5. jackal123uk thread starter macrumors member

    Joined:
    Jan 13, 2007
    Location:
    England
    #5
    Thanks for the pointers - I'll have a play around after I finish work and hopefully nail it.
     

Share This Page