I'm trying to share strings and integers throughout multiple views for a final project in an introductory iOS development course, around 50, and am wondering how I would go about doing this... I know the MVC paradigm is best but we didn't get to very advanced stuff, especially core data. I was thinking of using NSUserDefaults, I know it's wrong but this app doesn't have to be blazingly fast, and I think this would be the simplest way for me. My question is, where and how would I declare the NSUserDefault object I would be using? The only time in class that we used it was in one view. If I declare it in the first view that's loaded I know I can access it with the other views but do I need to import the header file of the first view into each of the others or will it be accessible regardless?
Update
So I thought I would try to make things easier by making NSUserDefaults
a property in all of the ViewControllers
. Then to attempt to get the values back I implemented the headers from each of the ViewControllers
into the view that needs to access the data stored in NSUserDefaults (which also has an NSUserDefault
property). The app runs terrifically until I get to that screen and nothing is updated. I'm sure my mistake lies somewhere in how I implemented the NSUserDefaults
but I am unsure of how to do it correctly. When we went over them in class (actually it's a Directed Study with "Sam's Teach Yourself iPhone Application Development in 24 Hrs" (puke, it makes me rage)), the only time we used them was in a single view application that changed the alpha of a white background so it could be used as a flash light. This is how it was presented:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger: var forKey: kVar];
[userDefaults synchronize];
This is what I was attempting in my app:
In my ViewController.h
files for EACH view:
...
@interface AppetizerViewController : UIViewController{
NSUserDefaults *userDefaults;
...
}
@property(assign)NSUserDefaults *userDefaults;
Then when I clicked a button I wanted to save that value to userDefaults
:
-(IBAction)plusEgg:(id)sender{
eggs++;
eggString = [NSString stringWithFormat:@"%d",eggs];
eggQty.text = eggString;
eggOrderedQty.text = eggString;
[userDefaults setInteger:eggs forKey:kEggQty];
[userDefaults synchronize];
[self updateAppSubtotal];
}
-(IBAction)minusEgg:(id)sender{
eggs--;
if(eggs < 0){
eggs = 0;
}
eggString = [NSString stringWithFormat:@"%d",eggs];
eggQty.text = eggString;
eggOrderedQty.text = eggString;
[userDefaults setInteger:eggs forKey:kEggQty];
[userDefaults synchronize];
[self updateAppSubtotal];
}
Then, since I had all of the constants for the NSUserDefault Keys
in the header files of all of the views, I implemented all of the header files into the BillViewController
like this and tried accessing them like this:
-(void)populateLabels{
pieFinalQty.text = [NSString stringWithFormat:@"%d",
[userDefaults integerForKey:kPieQty]];
hazFinalQty.text = [NSString stringWithFormat:@"%d",
[userDefaults integerForKey:kHazQty]];
briFinalQty.text = [NSString stringWithFormat:@"%d",
[userDefaults integerForKey:kBriQty]];
choFinalQty.text = [NSString stringWithFormat:@"%d",
[userDefaults integerForKey:kChoQty]];
iceFinalQty.text = [NSString stringWithFormat:@"%d",
[userDefaults integerForKey:kIceQty]];
}
-(void)getSubtotal{
NSString *preSubString;
NSString *subString;
subFloat = [userDefaults floatForKey:kAppSub]+[userDefaults floatForKey:kBevSub]+
[userDefaults floatForKey:kDesSub]+
[userDefaults floatForKey:kEntSub]+[userDefaults floatForKey:kSidSub];
if(subFloat < 10){
preSubString = [[NSString alloc]initWithFormat:@"%1.2f",subFloat];
}else if(subFloat < 100 && subFloat >= 10){
preSubString = [[NSString alloc]initWithFormat:@"%2.2f",subFloat];
}else if(subFloat < 1000 && subFloat >= 100){
preSubString = [[NSString alloc]initWithFormat:@"%3.2f",subFloat];
}else{
preSubString = [[NSString alloc]initWithFormat:@"%4.2f",subFloat];
}
subString = [[NSString alloc]initWithFormat:@"$%@",preSubString];
billSubtotal.text = subString;
[preSubString release];
[subString release];
}
I'm sorry that what you guys tried to explain to me was completely over my head apparently and that my naming conventions are kinda f***ed, but I've been working on this for about 15 hours straight with few breaks and it's about 4:00am here in MN. How can I go about this? I'm sorry too that it might have to be kind of explicit on how to implement it.