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.
Related
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.
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
I'm trying to figure out how to save the state of a UIScrollView. For example, I have several images and Labels in a UIScrollView that are loaded from xib.
Now my question is how can I make it when the user returns to this page the state of scrollview is the same that they stopped scrolling at? Does anyone know how to accomplish this? Thanks.
I've found this:
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
[defaults setFloat: scrollview.contentOffset.x forKey: #"myScrollPositionX"];
[defaults setFloat: scrollview.contentOffset.y forKey: #"myScrollPositionY"];
and for restore:
scrollView.contentOffset = CGPointMake([defaults floatForKey: #"myScrollPositionX"], [defaults floatForKey: #"myScrollPositionY"]);
Where should I implement this? Sorry I'm a newcomer, so pls make it simple :D
You can save the position to the defaults in the viewWillDisappear:animated method of your view controller, and set the content offset in the viewWillAppear:animated method, so the state is saved as the user leaves the view, and is restored just before the user re-enters the view.
I changed UINavigationBar's height by changing its frame.
But it's height restores to its original size if I send application to background and then open it again. How can I fix that?
You could write the frame to a resource file like a plist or XML file. Then load the plist in the app delegate file in either the -(void)applicationWillEnterForeground:(UIApplication *)application' or-(void)applicationDidBecomeActive:(UIApplication *)application`. I believe you could also use NSUserDefaults:
NSUserDefaults *frameSize = [NSUserDefaults standardUserDefaults];
//Put frame size into defaults
[sharedDefaults setObject:navigationBar.frame forKey:#"frame"];
[sharedDefaults synchronize];
Then in the aforementioned App Delegate methods, you could do something like this:
self.viewController.navigationController.frame = [sharedDefaults objectForKey:#"frame"];
Hope this helps and if it did, remember to up vote.
-----------EDIT------------
If you look at this question iOS selected tab, it tells you how to determine which tab is selected. What they basically say is to first add self.tabController.delegate = self in the app delegate, and make sure that the app delegate and the view controller with the tab bars conform to the UITabBarControllerDelegate protocol. Then in your View Controller file with the tab bars, you can use this method defined in the protocol:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 0) {
//The 0 is the first tab, but you can replace that with any other tab's index
//Here you say that when said tab is selected, the navigation controller's frame is the stored length.
self.navigationController.frame = [sharedDefaults objectForKey:#"frame"];
}
else{
self.navigationController.frame = CGRectMake(frame, for, other, tabs);
}
}
I have a couple of images that I would like to display the first time a user runs the app to show the user how to effectively use the app. What is the best method to do this sort of thing?
Thanks
The easiest way is to set a flag that says you have shown the app instructions. You can store them in user defaults.
So you would put something like this in your app delegate.
static NSString* const kAppHasShownStartupScreen = #"kAppHasShownStartupScreen";
BOOL hasShownStartup = [[NSUserDefaults standardUserDefaults] boolForKey:kAppHasShownStartupScreen];
if(hasShownStartup)
{
window.rootViewController = //your normal startup view controller
}
else
{
window.rootViewController = //your new view controller with instructions
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kAppHasShownStartupScreen];
}
Create some boolean values to store in NSUserDefaults or in Core Data that represent whether or not the user has viewed the 'tutorial'. Show the images by loading them into UIImageView's and adding them as subviews if the flag is false. Set the flag to true after they have viewed the images.
You can accomplish this functionality by using a BOOL value in NSUserDefaults:
#define appdb ((NSUserDefaults *)[NSUserDefaults standardUserDefaults])
if(![appdb boolForKey:#"applicationHasRunBefore"]) {
[appdb setBool:YES forKey:#"applicationHasRunBefore"];
[appdb synchronize];
...
// Do the tutorial
}