I have a StateArray.plist set up with 50 dictionary entries each containing a string with the name of the state and a number 0. What I want to be able to do is change the 0 to a 1 through a segmented control and save the updated plist and segmented control position. The code I have is below.
Here is my .h file
#import <UIKit/UIKit.h>
#import "StateConstants.h"
@interface StatesDetailViewController : UIViewController {
NSDictionary *states;
IBOutlet UILabel *nameOfStateTextField;
UISegmentedControl *visitedSC;
NSUInteger selected;
UIButton *save;
}
@property (nonatomic, retain) NSDictionary *states;
@property (nonatomic, retain) UILabel *nameOfStateTextField;
@property (nonatomic, retain) IBOutlet UISegmentedControl *visitedSC;
@property (nonatomic, retain) IBOutlet UIButton *save;
-(IBAction)visitedChange;
-(IBAction)saveButton:(id)sender;
@end
Here is my .m file
@implementation StatesDetailViewController
@synthesize nameOfStateTextField, visitedSC, states, save;
-(void) viewWillAppear: (BOOL) animated
{
[super viewWillAppear:animated];
nameOfStateTextField.text = [states objectForKey:NAME_KEY];
}
-(IBAction)visitedChange
{
selected = visitedSC.selectedSegmentIndex;
}
-(IBAction)saveButton:(id)sender
{
NSLog(@"Save pressed");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"StateArray.plist"];
NSNumber *newValue = [[NSNumber alloc] initWithInteger:selected];
[states setValue:newValue forKey:THERE_KEY];
[states writeToFile:path atomically:YES];
[newValue release];
}
User @thatsdisgusting helped me earlier with the NSNumber part which got rid of the errors. Now the app loads but the changed number doesn't save the segmented control's position. Also when I exit the app and open it back up, the entire table view is gone. This whole saving data on the iPhone is confusing me. Any help will be greatly appreciated. Thanks