Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

drf1229

macrumors regular
Original poster
Jun 22, 2009
237
0
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.
 

North Bronson

macrumors 6502
Oct 31, 2007
395
1
San José
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?
 

firewood

macrumors G3
Jul 29, 2003
8,107
1,345
Silicon Valley
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.
 

amorya

macrumors 6502
Jun 17, 2007
252
7
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.

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
 

firewood

macrumors G3
Jul 29, 2003
8,107
1,345
Silicon Valley
On the iPhone, like it or not, you're coding on an object oriented system.

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.

Trying to fight against that will get you nowhere.

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.
 

mccannmarc

macrumors 6502
Aug 15, 2008
270
0
Manchester, UK
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

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:
 

firewood

macrumors G3
Jul 29, 2003
8,107
1,345
Silicon Valley
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.

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.
 

mccannmarc

macrumors 6502
Aug 15, 2008
270
0
Manchester, UK
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.

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
 

bitfactory

macrumors 6502
Jul 22, 2002
346
390
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. ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.