Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-27-2011, 08:32 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 2
Gallimattias is on a distinguished road
Default Retain score value in labels between views

I'm making a quiz app for the iPhone with two labels simply showing how many questions the user answered correct/wrong. When the player presses one of the answers, the label's value goes up one point.

I have tried to make the code global, so that it's retained between the views, coding it in the AppDelegate. The thing is that the labels are empty when going to the new view/question. When one of the answers are pressed, the correct updated value is shown in the labels, but then disappear when going to a new question. What should I do?

Explanation:

Fel = Wrong answer/label

Ratt = Correct answer/label


AppDelegate.h

@class StartViewController;

@interface StartAppDelegate : NSObject <UIApplicationDelegate> {

}

@property (nonatomic, readwrite) int fel;
@property (nonatomic, readwrite) int ratt;

@end


AppDelegate.h

@implementation StartAppDelegate

@synthesize window=_window;

@synthesize ratt;

@synthesize fel;


ViewController.h

@class StartAppDelegate;

@interface Fraga4ViewController : UIViewController {

StartAppDelegate *app;

IBOutlet UILabel *labelFel;
IBOutlet UILabel *labelRatt;

}

-(IBAction)Ratt;
-(IBAction)Fel;

@end


ViewController.m

#import "StartAppDelegate.h"

@implementation Fraga4ViewController

- (void)viewDidLoad {

app = [[UIApplication sharedApplication]delegate];

[super viewDidLoad];
}

-(IBAction)Ratt; {

app.ratt = app.ratt +1;
labelRatt.text = [NSString stringWithFormat:@"%i", app.ratt];

}

-(IBAction)Fel; {

app.fel = app.fel +1;
labelFel.text = [NSString stringWithFormat:@"%i", app.fel];

}
Gallimattias is offline   Reply With Quote
Old 08-27-2011, 09:33 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Gallimattias View Post
I'm making a quiz app for the iPhone with two labels simply showing how many questions the user answered correct/wrong. When the player presses one of the answers, the label's value goes up one point.

I have tried to make the code global, so that it's retained between the views, coding it in the AppDelegate. The thing is that the labels are empty when going to the new view/question. When one of the answers are pressed, the correct updated value is shown in the labels, but then disappear when going to a new question. What should I do?

Explanation:

Fel = Wrong answer/label

Ratt = Correct answer/label


AppDelegate.h

@class StartViewController;

@interface StartAppDelegate : NSObject <UIApplicationDelegate> {

}

@property (nonatomic, readwrite) int fel;
@property (nonatomic, readwrite) int ratt;

@end


AppDelegate.h

@implementation StartAppDelegate

@synthesize window=_window;

@synthesize ratt;

@synthesize fel;


ViewController.h

@class StartAppDelegate;

@interface Fraga4ViewController : UIViewController {

StartAppDelegate *app;

IBOutlet UILabel *labelFel;
IBOutlet UILabel *labelRatt;

}

-(IBAction)Ratt;
-(IBAction)Fel;

@end


ViewController.m

#import "StartAppDelegate.h"

@implementation Fraga4ViewController

- (void)viewDidLoad {

app = [[UIApplication sharedApplication]delegate];

[super viewDidLoad];
}

-(IBAction)Ratt; {

app.ratt = app.ratt +1;
labelRatt.text = [NSString stringWithFormat:@"%i", app.ratt];

}

-(IBAction)Fel; {

app.fel = app.fel +1;
labelFel.text = [NSString stringWithFormat:@"%i", app.fel];

}
Add code to your view controller's viewWillAppear methods that convert the score to a string and display it in the label. That's standard procedure.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-27-2011, 09:39 AM   #3 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 2
Gallimattias is on a distinguished road
Default Ok

Thanks for your answer. I'm a bit of a noob. How do you write this code?

Quote:
Originally Posted by Duncan C View Post
Add code to your view controller's viewWillAppear methods that convert the score to a string and display it in the label. That's standard procedure.
Gallimattias is offline   Reply With Quote
Old 08-27-2011, 10:18 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Gallimattias View Post
Thanks for your answer. I'm a bit of a noob. How do you write this code?
Oh come on!

Add this line, that you already wrote (or copy-and-pasted) to your viewWillAppear method:


Code:
labelFel.text = [NSString stringWithFormat:@"%i", app.fel];


If you don't have a viewWillAppear method, search for it in the Xcode documentation. Copy the method definition into your .m file, and insert the line of code above.

You need to get a book on beginning Objective C/iOS development and do some reading. If you are as much of a noob as that, you're going to drive yourself (and us) crazy with not knowing how to do anything.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, label, uilabel, view, xcode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 394
15 members and 379 guests
7twenty7, chiataytuday, cristofercolmbos, dedeys78, fiftysixty, gmarro, iOS.Lover, jonathandeknudt, kilobytedump, Matrix23, raymng, ryantcb, stanny, UMAD, xerohuang
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:16 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0