I have a UIPickerView which displays the array of items. When i select an item, it can be passed to all my view controller. Now my problem is after moving to different view controllers and returned back to the UIPIckerView it shows the first array item and not the item which i selected. How can i view the selected item?
//in viewDidLoad()
itemsArray = [[NSArray alloc] initWithObjects:#"5", #"10", #"20", #"30", #"50",#"100", nil];
// if i select 20 and moved to other pages of my controllers and
// return to the UIPickerView i can see the 5 as selected not the 20
Any suggestions?
Hi use NSUserDefaults for holding the current selected value using ,
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithInt:albumCount] forKey:#"AlbumCount"];
[defaults synchronize];
for retrieving sake ,
albumCount = [defaults integerForKey:#"AlbumCount"];
Can do it in different ways
Use pass by value or reference in different VCs
Store the selected value in some other place which is persistant throughout the runtime and use it
Like
NSUserdefaults,
Inside a singleton instance[bad idea,memory retained all time but
still an option]
SQLite DB [ok ok,still hard in this case alone]
coredata [ sofisticated in this purpose]
In a file [write and read from file operations]
You will need to store the selected value somewhere, either as a property on a view controller or model, or in a central location like NSUserDefaults.
Then, when you return to that view controller with the UIPickerView you can use
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated to set the value.
Depending on what you want to achieve, and how you're approaching opening a further view it may be as simple as declaring #property int currentIndex, setting currentIndex = indexForSelectedItem and then in your viewWillAppear using it to alert your view what the last selected was.
Related
I'm working in a book app, and I want to use a page marker to help the user remember where he stopped reading. So I have added a bar button with an image ("mark.png"). A mark view will be added to the chapter view when it is tapped, and if it is been tapped again the mark will be removed from the superView. I'm using this code:
- (void)showMark {
if (![markView superView]) {
[chapterOne addSubView:markView];
}
else {
[markView removeFromSuperView];
}
}
It is working fine but ever time I exit the app and rerun again the mark view is gone, so how can I keep it?
I found some forums talking about the NSUserDefaults to save actions but I really don't know how to use it with my code. Any help will be appreciated.
You can't use NSUserDefaults to save entire views, but you can save the parmeters that would help determine where the bookmark should be set.
For example if you are basing the book mark by a page number you could save the page to the NSUserDefaults when the user leaves the view controller.
Example:
[[NSUserDefaults standardUserDefaults] setInteger:23 forKey:#"bookMarkPage"];
[[NSUserDefaults standardUserDefaults] synchronize];
When the user comes back the the view controller you can check if there is a bookmark:
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"bookMarkPage"] != nil) {
int pageNumber = [[NSUserDefaults standardUserDefaults] objectForKey:#"bookMarkPage"];
[self setBookmarkForPage:pageNumber];
}
Possible bookmark construction method:
- (void) setBookmarkForPage:(Int)pageNumber {
// run through the logic of placing the bookmark on the correct page
}
You can use whatever parameters you need to determine where to place the book mark. When a user originally places the bookmark what parameters you use to figure out where to place the bookmark? Try to use the same logic for when a user first places the bookmark.
I don't know exactly what you want to save, but you can just about any kind of data with NSUserDefaults, like this:
[[NSUserDefaults standardUserDefaults] setInteger:123 forKey:#"CurrentPageNumber"];
When you have set all the values you need, save them:
[[NSUserDefaults standardUserDefaults] synchronize];
Then when app opens, check to see if the value is set. If it is draw your marker.
if ([defaults valueForKey:#"CurrentPageNumber"] != nil) {
int pageNumber = [defaults valueForKey:#"CurrentPageNumber"]
if (pageNumber == 1) {
[chapterOne addSubView:markView];
}
else {
[markView removeFromSuperView];
}
}
The other answers state great ways to work around the issue. Just to clarify, UIView or any of the derivatives are not supported for NSUserDefaults. NSUserDefaults allows just primitive object types (NSString, NSNumber, NSArray, and NSDictionary). There might be one or two I missed. But UIView or UIViewController object types can't be saved in NSUserDefaults.
Can i add some images(3) on scroll view to flip them as page controller and include them with the splash, so that they only appear when someone install's the application or when newer version is installed... is their a way of doing it programmatically instead of adding xib .. any help ... coding will be much appreciated .. Thanks in advance
You cant show them along with the Default launch image. You can only show a static image there. But when the user is using the app for the first time, you can show this particular view once the app is launched and then from second time onwards you can disable it. You can set a property in NSUserDefaults for this once you have shown this view to the user so that from second time onwards, user wont see it again.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *string = [defaults objectForKey:#"didShowCustomView"];
if ([string isEqualToString:#"YES"]) {
//show the custom view
//once it is shown, set the value in user defaults
[defaults setObject:#"YES" forKey:#"didShowCustomView"];
[defaults synchronize];
}
This one worked for me as NSUserDefault really did the trick ..
But i had to put the loop in reverse for working it out in my very first view controller i did like this ...
- (void)viewDidLoad {
NSString *type = [[NSUserDefaults standardUserDefaults] objectForKey:#"myText1"];
if([type isEqualToString:#"Kill"]) {
// put the method of view like images,buttons or anything you have in your method
// for loading on to the first view that you want to run after splash
}
if(!type) {
type = #"Kill";
[[NSUserDefaults standardUserDefaults] setObject:type forKey:#"myText1"];
// put the code for your splash image here ..
}
}
So now the splash will only run once the app get's installed as a fresh copy ..
This one Helped me out ... if any one looking for the same thing can try my code above ..
Thank you
This is on a sidenote to my previous question about ratingview
I have this code:
[starView displayRating:1.5];
...however, I want it to be changed according to this:
-(void)ratingChanged:(float)newRating {
ratingLabel.text = [NSString stringWithFormat:#"Rating is: %1.1f", newRating];
//....
How can I make it displayRating: newString...
Can anyone show me how to do this please?
Update
Update
i have this code in place from what ive seen from your instructions (without the labels those are irrevelant)
-(void)viewDidLoad {
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSLog(#"Initializing rating view");
[starView setImagesDeselected:#"0.png" partlySelected:#"1.png" fullSelected:#"2.png" andDelegate:self];
[starView displayRating:[[defaults floatForKey:#"Rating"] stringValue]]; }
however i am getting an error message incompatible type for argument 1 of display rating and cannot convert to pointer type errors while building...
If you want to save a simple one value rating between application launches you should use NSUserDefaults like this:
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
// Save
[defaults setFloat:newRating forKey:#"Rating"];
// Read
float *savedRating=[defaults floatForKey:#"Rating"];
Update:
im sorry im a 16 year old and i have
no idea how to implement this into my
code. ive never used the nsuserdefault
class before i have this code how
would i do it.
-(void)viewDidLoad {
[super viewDidLoad]
theLabel.text = myLabel
theLabel2.text =myLabel2
NSLog(#"Initializing rating view")
[starView setImagesDeselected:#"0.png" partlySelected:#"1.png" fullSelected:#"2.png" andDelegate:self]
[starView displayRating:savedRating]
}
Well, to start, you would most likely want to put this code in viewDidAppear instead so that it is called every time the view appears. viewDidLoad is only called the first time the view controller loads from the nib file.
Suppose you wanted to set the text of theLabel to the value of the saved rating. It would look like this:
-(void)viewDidLoad {
[super viewDidLoad]
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
theLabel.text = [[defaults floatForKey:#"Rating"] stringValue];
theLabel2.text =myLabel2
NSLog(#"Initializing rating view")
[starView setImagesDeselected:#"0.png" partlySelected:#"1.png" fullSelected:#"2.png" andDelegate:self]
[starView displayRating:savedRating]
}
In my viewDidLoad, I'm retrieving user preferences and updating the settings tab. It is working for editSelection (see code) but the other
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL editSelection = [userDefaults boolForKey:#"editToggleSwitch"];
editingToggle.selectedSegmentIndex = editSelection; // this is working!
bToggle.selectedSegmentIndex = [[userDefaults objectForKey:#"bSegment"] intValue];
In the view, the editingToggle segment is displaying correctly but bToggle is always at 0? NSLog shows that the data was saved and retrieved correctly. I've even set
bToggle.selectedSegmentIndex = 1;
but it still does not reflect correctly in the view? Any ideas?
Just guessing: bToggle not wired up in Interface Builder?
One other gotcha. You can't put the initialization code for the segmented controls in the init because the view is not on the screen yet. I put it in viewDidLoad and it works fine now.
hi i want create an application and my app remember last user action ,, it means user find himself in the same page as he was last time he ran the application .
how could implement that ?
you can use the NSUserDefaults to store some variable for your application to retrieve them after a close of the application.
For exemple:
// Set the name of the view
[[NSUserDefaults standardUserDefaults] setObject:#"viewName" forKey:#"lastViewName"];
To retrieve the last action of the user:
// Retrieve the last view name
NSString *lastViewName = [[NSUserDefaults standardUserDefaults] stringForKey:#"lastViewName"];
But you can add other than a string.
Edit:
define some constant like that on a header file:
#define HOME_VIEW 0
#define VIEW1 1
#define VIEW2 2
When a view is load you store the current constant view to your standardUserDefaults. For example the view1:
- (void)viewDidLoad {
[super viewDidLoad];
// Set the name of the view
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:VIEW1] forKey:#"lastView"];
}
When you application did load you retrieve your constant and you load your appropriate view:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Retrieve the last view name
NSInteger constantView = [[NSUserDefaults standardUserDefaults] integerForKey:#"lastView"];
switch(constantView) {
case HOME_VIEW : //load your UIViewController
break;
case VIEW1 : // load your VIEW1
break;
//...
}
}
I haven't tested this code, but it's something like that to do.
Use the Application Delegate and override the method:
- (void)applicationWillTerminate:(UIApplication *)application
In here you can save which view was last visible and what state it was in (and if necessary other views as well) and persist it.
Then when you start your application again you load the saved state and use it to determine which view should be visible.
There are a few ways to persist data in the iPhone SDK so please read this guide for more information on how to do that.