PDA

View Full Version : iPhone app development question




gizabo
Apr 12, 2009, 08:39 AM
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



gizabo
Apr 13, 2009, 11:27 AM
can someone please help?

BlackWolf
Apr 13, 2009, 11:34 AM
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
Apr 13, 2009, 03:57 PM
so like this?


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


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

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

is that saying set the variable?

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

BlackWolf
Apr 13, 2009, 04:52 PM
so like this?


[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
Apr 13, 2009, 05:16 PM
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?


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




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


}

gizabo
Apr 13, 2009, 05:18 PM
i get these errors


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

CocoaPuffs
Apr 13, 2009, 05:49 PM
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
Apr 13, 2009, 06:18 PM
1) Where and how is prefs declared?

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

gizabo
Apr 13, 2009, 11:45 PM
lmao omg im an idiot sorry

ok i fixed it


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

eddietr
Apr 14, 2009, 12:19 AM
but im still getting an error

Sounds like you're trying to do something in global scope. Can you post your code?

gizabo
Apr 14, 2009, 09:48 AM
here :D

Btw, thanks for helping me guys :cool:


#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



#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

dejo
Apr 14, 2009, 10:07 AM
Your prefs declaration is not in the correct place.

BlackWolf
Apr 14, 2009, 10:19 AM
you should look at how the interface looks in objective-c 2.0 ...


#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