Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-20-2010, 11:26 PM   #1 (permalink)
k2c
Registered Member
 
Join Date: Oct 2009
Posts: 26
k2c is on a distinguished road
Unhappy UIPickerView NSUserDefaults Save Previous State

Hi
I have a problem save previous state of pickerview(UITextView).
I could save state of previous "row" on UIPickerView. It does go back and animate to previous "row", but it doesn't save (or activate) state of previous UITextView. PLease help!

Code:
#import "pickerviewtestViewController.h"

@implementation pickerviewtestViewController

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *) thePickerView;
{
	return 1;
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
	NSLog(@"Selected item: %@. Index of selected item: %i",[list objectAtIndex:row], row);
	
	
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
	[defaults setInteger:row forKey:@"pickerRow"]; 
	[defaults synchronize];

	
	if (row == 0) {
		textView.text = @"You selected the first one";
	}
	if (row == 1) {
		textView.text = @"You selected the second";
	}
	if (row == 2) {
		textView.text = @"You selected the three";
	}
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component;
{
	return[list count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
	return[list objectAtIndex:row];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	list = [[NSMutableArray alloc] init];
	[list addObject:@"Hello!!"];
	[list addObject:@"Hi Everyone!"];
	[list addObject:@"Konnichiwa!"];

	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[pickerView selectRow:[defaults integerForKey:@"pickerRow"] inComponent:0 animated:YES];
	
}
k2c is offline   Reply With Quote
Old 06-23-2010, 09:35 AM   #2 (permalink)
k2c
Registered Member
 
Join Date: Oct 2009
Posts: 26
k2c is on a distinguished road
Default

Anybody?
k2c is offline   Reply With Quote
Old 06-23-2010, 10:24 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by k2c View Post
Hi
I have a problem save previous state of pickerview(UITextView).
I could save state of previous "row" on UIPickerView. It does go back and animate to previous "row", but it doesn't save (or activate) state of previous UITextView. PLease help!

Code:
#import "pickerviewtestViewController.h"

@implementation pickerviewtestViewController

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *) thePickerView;
{
	return 1;
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
	NSLog(@"Selected item: %@. Index of selected item: %i",[list objectAtIndex:row], row);
	
	
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
	[defaults setInteger:row forKey:@"pickerRow"]; 
	[defaults synchronize];

	
	if (row == 0) {
		textView.text = @"You selected the first one";
	}
	if (row == 1) {
		textView.text = @"You selected the second";
	}
	if (row == 2) {
		textView.text = @"You selected the three";
	}
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component;
{
	return[list count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
	return[list objectAtIndex:row];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	list = [[NSMutableArray alloc] init];
	[list addObject:@"Hello!!"];
	[list addObject:@"Hi Everyone!"];
	[list addObject:@"Konnichiwa!"];

	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[pickerView selectRow:[defaults integerForKey:@"pickerRow"] inComponent:0 animated:YES];
	
}
Come on, dude. This is basic programming. Your program isn't setting the text view when you first display the pcker because the only place your code sets that textView field is in your pickerView:didSelectRow:inComponent: method, which only gets called when the USER picks a component in the picker.

To fix this problem, extract the code to set the text field value, and make it a separate method:

Code:
-(void) setTextFieldForRow: (NSInteger) rowIndex:
{
	if (rowIndex == 0) {
		textView.text = @"You selected the first one";
	}
	if (rowIndex == 1) {
		textView.text = @"You selected the second";
	}
	if (rowIndex == 2) {
		textView.text = @"You selected the three";
	}
}
Now call that method from both your -viewDidLoad method and from the pickerView:didSelectRow:inComponent: method.

Learn to think for yourself and debug your code. Use the debugger to set breakpoints and figure out the flow of control through your program. If you need to, add NSLog statements to figure out what methods get called in response to different user actions, and in what order.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 06-23-2010, 10:31 PM   #4 (permalink)
k2c
Registered Member
 
Join Date: Oct 2009
Posts: 26
k2c is on a distinguished road
Default

Thank you very much for spending time to answer my question, Duncan!
I'm really appreciated.

Sorry, I'm still new to program...
Could you kindly tell me how to call setTextFieldForRow method?
I don't know how to do that.
k2c is offline   Reply With Quote
Reply

Bookmarks

Tags
nsuserdefaults, previous, save, state, uipickerview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 332
8 members and 324 guests
anothermine, Chickenrig, Domele, givensur, heshiming, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:18 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0