Hello Everyone!
Brand new programmer here, so off-topic help/constructive criticism welcome.
I am saving a default state (such as "New York") in a UIPickerView which is in a FlipSideView. It certainly saves for the first and second time I check to make sure it's the same state (I am clicking "done" and then "info" repeatedly), but on the third check the picker has moved up three states (to "New Hampshire") and then every time I check the picker progresses three more states.
Here's the .h and .m files of the FlipSideViewController:
#import <UIKit/UIKit.h>
import "Calculator.h"
@protocol FlipsideViewControllerDelegate;
@interface FlipsideViewController : UIViewController {
id delegate;
UIPickerView *myPickerView;
NSArray *pickerViewArray;
}
@property (nonatomic, assign) id delegate;
@property (nonatomic, retain) UIPickerView *myPickerView;
@property (nonatomic, retain) NSArray *pickerViewArray;
(IBAction)done;
@end
@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end
import "FlipsideViewController.h"
@implementation FlipsideViewController
@synthesize delegate;
@synthesize myPickerView, pickerViewArray;
-(CGRect)pickerFrameWithSize:(CGSize)size;{
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGRect pickerRect = CGRectMake( 0.0,
screenRect.size.height - size.height,
size.width,
size.height);
return pickerRect;
}
(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
[self createPicker];
}
-(void)viewWillAppear:(BOOL)animated;{
[super viewWillAppear:NO];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[myPickerView selectRow:[defaults integerForKey:@"pickerRow"] inComponent:0 animated:NO];
}
-(void)createPicker;{
pickerViewArray = [[NSArray arrayWithObjects:
@"Alabama",@"Alaska", @"Arizona",@"Arkansas",@"California",@"Colorado",@"Connecticut",@"Delaware",
@"District of Columbia",@"Florida",@"Georgia",@"Hawaii",@"Idaho",@"Illinois",@"Indiana",@"Iowa",
@"Kansas",@"Kentucky",@"Louisiana",@"Maine",@"Maryland",@"Massachusetts",@"Michigan",
@"Minnesota",@"Mississippi",@"Missouri",@"Montana",@"Nebraska",@"Nevada",@"New Hampshire",@"New Jersey",
@"New Mexico",@"New York",@"North Carolina",@"North Dakota",@"Ohio",@"Oklahoma",
@"Oregon",@"Pennsylvania",@"Rhode Island",@"South Carolina",@"South Dakota",@"Tennessee",
@"Texas",@"Utah",@"Vermont",@"Virginia",@"Washington",@"West Virginia",@"Wisconsin",@"Wyoming",
nil] retain];
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
CGSize pickerSize = [myPickerView sizeThatFits:CGSizeZero];
myPickerView.frame = [self pickerFrameWithSize:pickerSize];
myPickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
myPickerView.showsSelectionIndicator = YES;
myPickerView.delegate = self;
myPickerView.dataSource = self;
[self.view addSubview:myPickerView];
}
(IBAction)done {
[self.delegate flipsideViewControllerDidFinish:self];
}
pragma mark -
pragma mark UIPickerViewDataSource
(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:row forKey:@"pickerRow"];
[defaults setObject:[pickerViewArray objectAtIndex:row] forKey:@"pickerString"];
return [pickerViewArray objectAtIndex:row];
}
(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:row forKey:@"pickerRow"];
[defaults setObject:[pickerViewArray objectAtIndex:row] forKey:@"pickerString"];
}
(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 240.0;
}
(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 40.0;
}
(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerViewArray count];
}
(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
call me mystified!
Thanks for any help. Please let me know if I can make myself more clear...