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 09-25-2010, 12:46 PM   #1 (permalink)
Velvet Rock Apps
 
Join Date: Sep 2010
Posts: 231
ChrisYates is on a distinguished road
Default Saving UILabel Text After App Quits

Hi,

This is probably easy but bear with me as I am new to iPhone development.

I'm presuming I need to do this in an array but can't figure out the method.

Basically I want to save the text in a UILabel once a user quits out of the application. The idea is that once the user relaunches the app, I can get the text from the UI label (whatever it was before quitting) and display a different view based on what the content of the UI label is.

So in other words if the UILabel said 'One' (before quitting) then on relaunch View1 would show and if the UILabel said 'Two' (before quitting) then on relaunch View2 would show etc etc.

Thanks for any help.
ChrisYates is offline   Reply With Quote
Old 09-25-2010, 12:52 PM   #2 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 577
Speed is on a distinguished road
Default

Quote:
Originally Posted by ChrisYates View Post
Hi,

This is probably easy but bear with me as I am new to iPhone development.

I'm presuming I need to do this in an array but can't figure out the method.

Basically I want to save the text in a UILabel once a user quits out of the application. The idea is that once the user relaunches the app, I can get the text from the UI label (whatever it was before quitting) and display a different view based on what the content of the UI label is.

So in other words if the UILabel said 'One' (before quitting) then on relaunch View1 would show and if the UILabel said 'Two' (before quitting) then on relaunch View2 would show etc etc.

Thanks for any help.
Well, you are first going to want to get the text of the label into an NSString.

Code:
NSString *myString = myLabel.text;
Now, we are going to want to save that string into NSUserDefaults.

Code:
[[NSUserDefaults standardUserDefaults]setObject:myString forKey:@"myString"];
To access it on startup, we will run this code:

Code:
-(void)viewDidLoad {

	NSString *myString = [[NSUserDefaults standardUserDefaults]objectForKey:@"myString"];

}
Now, we can check if the label is equal to something.

Code:
if([myString isEqualToString:@"myEqualStringText"]) {
//Do Stuff
}
I hope that works for you. If you have any questions, just ask!

Last edited by Speed; 09-25-2010 at 01:03 PM.
Speed is offline   Reply With Quote
Old 09-25-2010, 12:56 PM   #3 (permalink)
Velvet Rock Apps
 
Join Date: Sep 2010
Posts: 231
ChrisYates is on a distinguished road
Default

Thanks very much, I'll give that a go and see how it works. Much appreciated.
ChrisYates is offline   Reply With Quote
Old 09-26-2010, 07:15 AM   #4 (permalink)
Velvet Rock Apps
 
Join Date: Sep 2010
Posts: 231
ChrisYates is on a distinguished road
Default

So frustrating!

I tried the above NSString method but couldn't get it to work so I decided to go back to the basics and get a different window to show on startup and worry about saving the string later on. For some reason I can't even get this to work, I'm not sure if I've been looking at the code too long!! I'm obviously putting this in the applicationDidFinishLaunching method. Can anyone advise my tired brain?!...thanks.

Code:
{
		if(infoCount.text == @"Test 1"){
				[window addSubview:View1];
			}
			
		else if(infoCount.text == @"Test 2");{
			        [window addSubview:View2];
			}
		}
			
    [window makeKeyAndVisible];
	return YES;
}
ChrisYates is offline   Reply With Quote
Old 09-26-2010, 11:19 AM   #5 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Not_Appy_At_All is on a distinguished road
Default

Quote:
Originally Posted by ChrisYates View Post
So frustrating!

I tried the above NSString method but couldn't get it to work so I decided to go back to the basics and get a different window to show on startup and worry about saving the string later on. For some reason I can't even get this to work, I'm not sure if I've been looking at the code too long!! I'm obviously putting this in the applicationDidFinishLaunching method. Can anyone advise my tired brain?!...thanks.

Code:
{
		if(infoCount.text == @"Test 1"){
				[window addSubview:View1];
			}
			
		else if(infoCount.text == @"Test 2");{
			        [window addSubview:View2];
			}
		}
			
    [window makeKeyAndVisible];
	return YES;
}

You have to use the NSString method...it compares the two strings.


The == compares the two memory locations and not the strings...from my recollections on having the same problem as you.
Not_Appy_At_All is offline   Reply With Quote
Old 09-26-2010, 11:25 AM   #6 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 577
Speed is on a distinguished road
Default

Quote:
Originally Posted by Not_Appy_At_All View Post
You have to use the NSString method...it compares the two strings.


The == compares the two memory locations and not the strings...from my recollections on having the same problem as you.
In response to this, the method is isEqualToString:@"". Make sure to enclose this in [ ] (brackets) as that may cause your issue.
Speed is offline   Reply With Quote
Old 09-26-2010, 02:34 PM   #7 (permalink)
Velvet Rock Apps
 
Join Date: Sep 2010
Posts: 231
ChrisYates is on a distinguished road
Default

Okay I managed to get the correct view showing on application launch using:

Code:
	NSString *myString = myText.text;
	if([myString isEqualToString:@"Test 1"]) {
		[window addSubview:View2];
	}
	else if 
		([myString isEqualToString:@"Test 2"]){
		[window addSubview:View1];
}
The text may change depending on what the user does and before quitting I want to save the string, but with the code:
Code:
[[NSUserDefaults standardUserDefaults]setObject:myString forKey:@"myString"];
It doesn't seem to save and once reopening the application it defaults back to the default view.

Is there any other way I can save the string before the application quits and then open it up again upon launch.....thanks.
ChrisYates is offline   Reply With Quote
Old 09-26-2010, 02:56 PM   #8 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Not_Appy_At_All is on a distinguished road
Default

Quote:
Originally Posted by ChrisYates View Post
Okay I managed to get the correct view showing on application launch using:

Code:
	NSString *myString = myText.text;
	if([myString isEqualToString:@"Test 1"]) {
		[window addSubview:View2];
	}
	else if 
		([myString isEqualToString:@"Test 2"]){
		[window addSubview:View1];
}
The text may change depending on what the user does and before quitting I want to save the string, but with the code:
Code:
[[NSUserDefaults standardUserDefaults]setObject:myString forKey:@"myString"];
It doesn't seem to save and once reopening the application it defaults back to the default view.

Is there any other way I can save the string before the application quits and then open it up again upon launch.....thanks.
You need "synchronize" to save it...Google it. It is pretty straight forward.
Not_Appy_At_All is offline   Reply With Quote
Reply

Bookmarks

Tags
saving data, uilabel

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: 342
8 members and 334 guests
headkaze, mistergreen2011, nobstudio, Objective Zero, pungs, revg, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

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