Passing variables from class to class

Discussion in 'iOS Programming' started by drf1229, Jul 18, 2009.

  1. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #1
    I was wondering if there was a way to pass variables from one view to another. view1.m looks like this:
    Code:
    int lives=3;
    I want the second view to read the variable lives like this:
    Code:
    int lives=view1.lives;
    but its no good. I'm not even sure this is possible, but if anybody knows or has any suggestions, please share.
     
  2. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #2
    What relationship to these two different views have?

    Is one a sub-view of the other? Is one view an instance variable of another? Do they share a common view controller?

    Also, depending on what this "lives" variable represents, you might not want to code this in your view class. How about placing this in your model class, or even your controller class?
     
  3. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #3
    If you only have (and intend to have) one variable of this name and one instance of each type of view controller, just make your variable a global.

    All other syntactic sugar will just take more lines of code (providing more opportunities for typo bugs), and eat a tiny bit more battery power.

    Just make a note that if you want to reuse this code, it won't work as is.
     
  4. amorya macrumors 6502

    Joined:
    Jun 17, 2007
    #4
    That's bad advice. Don't do this.

    On the iPhone, like it or not, you're coding on an object oriented system. Trying to fight against that will get you nowhere.

    To the OP, I suggest you read an article about the Model-View-Controller design paradigm.

    Amorya
     
  5. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    Nonsense. You are coding for an armv6 or armv7 ISA CPU running a Unix operating system kernel. Almost any programming language will do (although some will keep you from getting accepted in the app store).

    The only OOP required is where one absolutely needs to interface with UIKit. Some top selling iPhone games do none of that once they load a Open GL ES view controller, the rest is standard C. Some audio apps spend most of their CPU time in code written in ARM VFP assembly language. Many UIWebview-encapsulated web apps are written in non-OOP-style Javascript. And etc.

    And I've seen cases where a targeted "fight against" OOP saved users a significant amount of iPhone battery life. Might as well learn how to use those techniques that right off the top. Global variables on digital computers have been around longer than you have, and successfully put a man on the moon 40 years ago.

    imho.
     
  6. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #6
    Thats ridiculous, there is nothing wrong with the use of global variables unless you are writing a 1,000,000 line app that you intend to port to other systems or a reusable code base etc.

    If for some reason you feel you have to appease the OOP gods then put your global variables in a singleton class, more work for barely any gain but hey its an object - its must be worth it :rolleyes:
     
  7. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #7
    I would put the limit a tad lower. It gets hard to maintain over 10K to 100K LOC without standardized coding practices for the project entire team.

    However the OP's app is probably nowhere near any of the above sizes. No need for the overhead of supporting a properly drilled military artillery team just to smash a fly on the wall.
     
  8. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #8
    I'll agree, the game we are currently developing is around the 11k LOC mark and it is getting more demanding to maintain. When I said 1,000,000 LOC I was exaggerating a little but you get the point
     
  9. bitfactory macrumors 6502

    bitfactory

    Joined:
    Jul 22, 2002
    #9
    While I'm still learning iOS programming - there's not nearly enough info in the question to answer. Are you going between views? The standard way would be to pass the info in the prepareforsegue method when moving from one to another.

    As far as the arguments up top - I was initially taught to use a singleton, but I'm sure they were just beating OOP into our skulls. For a tiny app I'd just declare a global var and be done with it. ;)
     
  10. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #10
    @bitfactory, It's a six year old thread. I'm sure he's worked it out by now. Start a new thread if you have a question.
     

Share This Page