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

gizabo

macrumors regular
Original poster
Jul 20, 2008
124
0
i have two questions :D

1) I need help saving my int variable into a plist and be able to read it out of the plist. How do i do that?

2) I have an image of a big red X (to symbolize you got something wrong) and a big green check mark ( to symbolize that you got something right). How do i make it so i can turn the image on or off?

thanks
 

BlackWolf

macrumors regular
Apr 9, 2009
244
0
1) best thing is to use NSUserDefaults. use setInteger:forKey: to set it and integerForKey: to get it

2) just use the hidden property every UIView has. set it to YES to hide it or NO to show it
 

gizabo

macrumors regular
Original poster
Jul 20, 2008
124
0
so like this?

Code:
[prefs setObject::highScore forKey:@"highScore"];

Note: highScore is an int, so when i write highscore here
[prefs setObject::highScore forKey:mad:"highScore"];
is that saying set the variable?

and is "highScore" in the quotes the name in which i call it?
 

BlackWolf

macrumors regular
Apr 9, 2009
244
0
so like this?

Code:
[prefs setObject::highScore forKey:@"highScore"];

Note: highScore is an int, so when i write highscore here

is that saying set the variable?

and is "highScore" in the quotes the name in which i call it?

you should take a look at how NSUserDefaults, NSDictionary or .plist themselves work. they are basic key-value-pairs. some stuff is wrong about your code

1) why two :'s - one is enough. guess it's just a typo
2) like you said yourself - highScore is an int. int's are not objects. so you must use setInteger:forKey: rather than setObject:forKey:
3) key-value-pair means you have a key, and with that key you can "talk" to the value that is assigned to that key. so yes, "highScore" is a string that is used to call your stored value afterwards with NSUserDefaults integerForKey: method

you should read some of the basics again I guess btw! "highScore in quotes" makes me believe you don't know what a string is - and you should definetly know that. just my impression, no offense
 

gizabo

macrumors regular
Original poster
Jul 20, 2008
124
0
no affence taken :), and i am fairley new at this

And i do know what a string was, i was just trying ot explain it easier sorry

hhhmm something is weird... Why are these giving me errors?

Code:
- (void)awakeFromNib {
	highScore = [prefs integerForKey:@"highScore"];
	srandom(time(NULL));
	[self setLevel:1 withMessage:nil];
}

Code:
if (level > highScore) {
		highScore = level;
		[scoreLabel setText:[NSString stringWithFormat:@"High Score: %i", highScore]];
		[prefs setInteger:forKey:highScore forKey:@"highScore"];
	}
	
	
}
 

gizabo

macrumors regular
Original poster
Jul 20, 2008
124
0
i get these errors

Code:
error: 'prefs' undeclared (first use in function)
error: 'forKey' undeclared (first use in function)
 

CocoaPuffs

macrumors 68020
Aug 23, 2008
2,005
3
Pick up a book and read it. At this rate, Apple will release 4.0 before you figure out how to write into plist.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
1) Where and how is prefs declared?

2)
Code:
[prefs setInteger:forKey:highScore forKey:@"highScore"];
Do you see anything immediately wrong with that?
 

gizabo

macrumors regular
Original poster
Jul 20, 2008
124
0
lmao omg im an idiot sorry

ok i fixed it

Code:
if (level > highScore) {
		highScore = level;
		[scoreLabel setText:[NSString stringWithFormat:@"High Score: %i", highScore]];
		[prefs setInteger:highScore forKey:@"highScore"];
	}

but im still getting an error


error: initializer element is not constant
 

gizabo

macrumors regular
Original poster
Jul 20, 2008
124
0
here :D

Btw, thanks for helping me guys :cool:

Code:
#import "DoorsViewController.h"
@implementation DoorsViewController


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
}

- (void)awakeFromNib {
	highScore = [prefs integerForKey:@"highScore"];
	srandom(time(NULL));
	[self setLevel:1 withMessage:nil];
}

- (IBAction)clickDoor:(id)sender {
	int correctDoor = random() % 3 + 1;
	
	if(correctDoor == [sender tag])
		
		[self setLevel:level + 1 withMessage:[NSString stringWithFormat:@"You passed! Welcome to level %i!", level + 1]];
	
	else {
		[self setLevel:1 withMessage:[NSString stringWithFormat:@"Sorry, you failed! The correct door was door #%i!", correctDoor]];
		wrongCount++;
		[wrong setText:[NSString	stringWithFormat:@"In this session you have gotten %d wrong", wrongCount]];
	}
}


- (void)setLevel:(int)newLevel withMessage:(NSString *)message {
	level = newLevel;
	[self setTitle:[NSString stringWithFormat:@"Level %i", level]];
	
	if (message)
		[successLabel setText:message];	
	
	if (level > highScore) {
		highScore = level;
		[scoreLabel setText:[NSString stringWithFormat:@"High Score: %i", highScore]];
		[prefs setInteger:highScore forKey:@"highScore"];
	}
	
	
}

- (IBAction)resetDoor:(id)sender
{
	level = 1;
	wrongCount = 0;
	[self setTitle:[NSString stringWithFormat:@"Level %i", level]];
	[wrong setText:[NSString stringWithFormat:@"In this session you have gotten %d wrong", wrongCount]];
}

@end

Code:
#import <UIKit/UIKit.h>

@interface DoorsViewController : UIViewController {
	int level;
	int highScore;
	int wrongCount;
	
	IBOutlet UILabel *scoreLabel;
	IBOutlet UILabel *successLabel;
	IBOutlet UILabel *wrong;
	

}

- (IBAction)resetDoor:(id)sender;
- (IBAction)clickDoor:(id)sender;
- (void)setLevel:(int)newLevel withMessage:(NSString *)message;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


@end
 

BlackWolf

macrumors regular
Apr 9, 2009
244
0
you should look at how the interface looks in objective-c 2.0 ...

Code:
#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController <UIActionSheetDelegate> {
	UILabel *label;
	UIButton *button;
	UIActivityIndicatorView *activityIndicator;
	UISlider *speedSlider;
	UISwitch *directionSwitch;
	UIButton *resetButton;
	NSTimer *timer;
	NSUserDefaults *defaults;
}

@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) IBOutlet UIButton *button;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
@property (nonatomic, retain) IBOutlet UISlider *speedSlider;
@property (nonatomic, retain) IBOutlet UISwitch *directionSwitch;
@property (nonatomic, retain) IBOutlet UIButton *resetButton;
@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic, retain) NSUserDefaults *defaults;

-(IBAction)buttonClicked:(id)sender;
-(void)countUpwards;
-(IBAction)resetButtonClicked:(id)sender;
-(IBAction)newSpeed:(id)sender;

@end
... at least that's how I learned it, hope it's not wrong :p
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.