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

skater618

macrumors newbie
Original poster
Aug 22, 2012
11
0
i will give you any info/pics of my code that you need to be able to help. with that being said it is a NSTimer that i am using for the score
 

Attachments

  • Screen Shot 2012-08-24 at 9.47.59 PM.png
    Screen Shot 2012-08-24 at 9.47.59 PM.png
    19.5 KB · Views: 160
We need to know more about what you're doing.

What do you want to have happen and what have you tried? What have your results been so far?
 
game

i am making a game and i have a score (NSTimer) and i have when the game is over it switches to a new view to show score and go back to the menu ect. i was wondering if any one knows how to have it show the score on the 2 view. i have tried to find stuff on the internet and i can't find anything. i have also done a little bit of looking into NSStrings but i don't fully understand if that is what i need or not to show the score in a UILabel.
 
I had to pass info from one view controller to another yesterday and this was my approach:
1. Made a class with a static variable, plus two static methods (one to write var, one to read)
2. Each view controller can call these methods without a pointer(reference) because they're static

There's probably cleaner ways, but it was a quick and dirty fix.
 
Ok so how would you put that into the code that I have because I have 2 int mainInt and mainInt2
 
Is the problem that you can find the value of those integers within one of the views,
or is it that you can't figure out how to print it to the screen?
 
Questions:
How much C do you know?
How much Obj-C do you know?
How much experience do you have with using Xcode to make Cocoa or Cocoa Touch apps?

Anyways, to display an integer on screen you'll want to add a UILabel to your nib or storyboard. Then hook it up to an outlet. Then you'll want to have a line of code that looks something like:

Code:
myLabel.text = [NSString stringWithFormat:%i, intToPrint];

I suspect that this doesn't help you very much though, because you lack adequate background. Once you answer the questions I started this post with I can start telling you where you need to go to start learning stuff.
 
i will give you any info/pics of my code that you need to be able to help. with that being said it is a NSTimer that i am using for the score

You have not described your problem well enough for anybody do help you.

What does it mean "it is a NSTimer that i am using for the score"? Does the score increase every time the timer goes off?

In general, you should use properties to communicate values between view controllers. You can then pass the score from one to the next:

viewController2.score = mainInt1;

If you don't know what properties are then you need to get a book on Objective C/iOS development and do some reading.
 
yes Duncan C the score goes up when the timer goes off and yes i have been reading in some beginner books but you said to use viewController2.score = mainInt1; that looks right now would i put that in the .h or .m. where do you think there are some good places to learn properties?

----------

ArtOfWarfare i don't really have a lot of experience and i started with objective-c
 
Last edited:
Can't you just use the listener observer pattern?

If he doesn't know how to define a property then Key Value Coding is going to be way, way over his head.

----------

yes Duncan C the score goes up when the timer goes off and yes i have been reading in some beginner books but you said to use viewController2.score = mainInt1; that looks right now would i put that in the .h or .m. where do you think there are some good places to learn properties?

----------

ArtOfWarfare i don't really have a lot of experience and i started with objective-c


Apple has a good pdf on Objective C, but it's a bit dry and information-dense:

The Objective C Programming Language

You might want to buy and read "Objective C, the Big Nerd Range Guide" by Aaron Hillegass It's a good beginner's book on the basics of Objective C. It assumes you don't know anything about programming, and teaches you from the very beginning.

If you know programming in another language and just need to learn Objective C then you might want a different book, since the concepts of for loops, while loops, if/then/else, etc, should be familiar.
 
ok thank you for all the help i will look up the books and stuff and i will read them thank you for the info
 
I haven't done anything with objective C but the observer pattern is very simply and in C++ is very easy to implement. There are examples all over the internet but basically there is an observer class that has a container of listeners. The listeners are basically interfaces that will implement a function to do stuff when for example state data changes. The classes that inherit from the interface will register with observer. The observer will insert the listener in the container when it registers. When there is a state data change for example the observer will notify the listeners in the container. It's a very simple OO design pattern and will work fine with what you want to accomplish. Again not sure if objective C is similar to C++ or Java but if it is then that pattern will work.
 
Skater, if you're coming at this knowing nothing at all, here are my suggestions:

1 - Learn C. Obj-C and C++ are just add-on languages to C. If you learn C, you know most of the other two languages already. Plus Java is based heavily on C.

To learn it, I suggest Learn C The Hard Way:
http://c.learncodethehardway.org/book/

It's a free ebook. If you have problems with it, feel free to ask here.

2 - Learn Obj-C/Xcode/Cocoa Touch. For that, I suggest Stanford's free iOS lectures on iTunes U.
 
The easiest, fastest method is to stick the integer in a C global variable. You can read global variables from anywhere (with an extern declaration).

There are other more robust methodologies for code you plan on maintaining, giving to someone else to maintain, or code that you plan on unit testing or reusing. But very often, that is not the case. Most student apps are thrown away (for good reason). Might as well have another good reason.
 
The easiest, fastest method is to stick the integer in a C global variable. You can read global variables from anywhere (with an extern declaration).

There are other more robust methodologies for code you plan on maintaining, giving to someone else to maintain, or code that you plan on unit testing or reusing. But very often, that is not the case. Most student apps are thrown away (for good reason). Might as well have another good reason.

Gak! That is an easy way to do it the same way that stretching a power wire across the floor from the oven is the easiest way to power your new dishwasher. It works, but you'll trip over that wire for the rest of your life, and your house might burn down next year because you've overloaded the house wiring.

Global variables create very tight binding between the different parts of your program. They destroy modularity. The end result is spaghetti-code. Difficult to figure out, difficult to maintain.

Student apps are for learning how to do things the right way, not how to write spaghetti-code.
 
Global variables create very tight binding between the different parts of your program. They destroy modularity. The end result is spaghetti-code. Difficult to figure out, difficult to maintain.

Exactly correct. Since student programs are never maintained and smaller than one module of many real apps, there is no reason not to use the simplest coding technique. Globals. No need to make Johnny learn 767 landing procedures at LAX to take the trike for a spin around the backyard.

Save it for apps that need it instead of wasting people's time and battery.
 
Exactly correct. Since student programs are never maintained and smaller than one module of many real apps, there is no reason not to use the simplest coding technique. Globals. No need to make Johnny learn 767 landing procedures at LAX to take the trike for a spin around the backyard.

Save it for apps that need it instead of wasting people's time and battery.

It depends on the goal of the student app. If the goal is to learn how to write REAL software, then teaching the student a dead-end solution that is poison to a real-world app is NOT a good idea.

If the student never wants to advance beyond throw-away tutorials, then fine.
 
skater618, here's a tip: Don't use images of code to provide code snippets. Use the
Code:
 tags. This makes it easier for other to see the code as well as provides an option for them to more-easily copy the code should they want to test something on their own.
 
thank you for all the help and i will keep the books in mind i am reading a few right now and by the way yes i am working on simple games right now but i intend to make much bigger as i get older so i will want to practice good habits now.

----------

is there anyway i could show you all the code that i do have now so you could help with the score part i learn better from making little things like this.
 
If the goal is to learn how to write REAL software...

The problem is that many people must learn to write software before learning to write REAL software. Confusing the two goals often leads to them accomplishing neither.

So I encourage writing really bad software, as more people learn something that way, and more total software gets written that way. Some of it even works (and not necessarily the stuff developed using "best practices").

Some of the kids who crash their trikes eventually get their commercial pilots license.
 
...i will keep the books in mind i am reading a few right now...

What books? When referencing other resources, it is common courtesy here to give specifics about those resources. In the case of books: title, author, edition, (and, if appropriate, chapter and page number). For online tutorials, URLs.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.