iPhone app development question

Discussion in 'iOS Programming' started by gizabo, Apr 12, 2009.

  1. gizabo
    Expand Collapse
    macrumors regular

    Joined:
    Jul 20, 2008
    #1
    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
     
  2. gizabo
    Expand Collapse
    thread starter macrumors regular

    Joined:
    Jul 20, 2008
  3. BlackWolf
    Expand Collapse
    macrumors regular

    Joined:
    Apr 9, 2009
    #3
    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
     
  4. gizabo
    Expand Collapse
    thread starter macrumors regular

    Joined:
    Jul 20, 2008
    #4
    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?
     
  5. BlackWolf
    Expand Collapse
    macrumors regular

    Joined:
    Apr 9, 2009
    #5
    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
     
  6. gizabo
    Expand Collapse
    thread starter macrumors regular

    Joined:
    Jul 20, 2008
    #6
    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"];
    	}
    	
    	
    }
    
    
     
  7. gizabo
    Expand Collapse
    thread starter macrumors regular

    Joined:
    Jul 20, 2008
    #7
    i get these errors

    Code:
    error: 'prefs' undeclared (first use in function)
    error: 'forKey' undeclared (first use in function)
    
    
     
  8. CocoaPuffs
    Expand Collapse
    macrumors 68010

    Joined:
    Aug 23, 2008
    #8
    Pick up a book and read it. At this rate, Apple will release 4.0 before you figure out how to write into plist.
     
  9. dejo
    Expand Collapse
    Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    1) Where and how is prefs declared?

    2)
    Code:
    [prefs setInteger:forKey:highScore forKey:@"highScore"];
    Do you see anything immediately wrong with that?
     
  10. gizabo
    Expand Collapse
    thread starter macrumors regular

    Joined:
    Jul 20, 2008
    #10
    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


     
  11. eddietr
    Expand Collapse
    macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #11
    Sounds like you're trying to do something in global scope. Can you post your code?
     
  12. gizabo
    Expand Collapse
    thread starter macrumors regular

    Joined:
    Jul 20, 2008
    #12
    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
    
    
     
  13. dejo
    Expand Collapse
    Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    Your prefs declaration is not in the correct place.
     
  14. BlackWolf
    Expand Collapse
    macrumors regular

    Joined:
    Apr 9, 2009
    #14
    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
     

Share This Page