Restore the state of UIScrollview - iphone

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.

Related

Showing UIPickerview selected value

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.

How to save the subView that has been added with NSUserDefaults?

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.

Adding Screen Before Splash

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

selectedSegmentIndex not updating UISegmentControl

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.

How can I get the UITableView scroll position so I can save it?

Is there any way to find out which UITableViewCell is at the top of the scroll window?
I'd like to get the current scroll position so that I can save it when the app exits. When the app gets started I want to scroll to the position it was at when it last exited.
You can easily grab the exact offset of the table view by looking at its contentOffset property. For the vertical scroll, look at:
tableView.contentOffset.y;
The accepted solution only works if you know the size of all table view items. With auto sizing/estimated size that is not always true.
One alternative is to save the first visible item and scroll to it.
You can get the first visible item indexPath with:
savedIndex = tableView.indexPathsForVisibleRows?.first
Then scroll to it by doing:
tableView.scrollToRowAtIndexPath(savedIndex, atScrollPosition: .Top, animated: false)
Make sure you can load in viewWillAppear and not viewDidLoad (tested iOS 9). ViewWillAppear is when view has finished layout - there is differences in the outcome.
-(void) viewWillAppear:(BOOL)animated {
NSUserDefaults *lightData = [NSUserDefaults standardUserDefaults];
[self.tableView setContentOffset:CGPointMake(0, [lightData floatForKey:#"yValue"])];
}
-(void) viewWillDisappear:(BOOL)animated {
NSUserDefaults *lightData = [NSUserDefaults standardUserDefaults];
[lightData setFloat:self.tableView.contentOffset.y forKey:#"yValue"];
[lightData synchronize];
}