I'm very new to objective c (three weeks) and also in my 70s so not as quick as I used to be! Anyway, I'm doing okay with an app for IOS except for a problem with maintaining slider position and value when switching away from a view and back again. To clarify: I have 3 views, each with 4 sliders and I want to be able to return to any view with the sliders positions and values the same as they were left. I'm sure there's a way of doing this but I've searched all over to no avail so I hope someone here can help!
Do you allocate an instance of a viewController every time you push it onto your navigation stack ? if so , create a property and allocate it only once and use the same object to push in onto your navigation stack. it should retain the position.
Another possible solution is to persist the current value of the slider in your viewController and set the slider value to the persisted value in viewWillAppear , which gets called every time the view comes on screen
You could save the sliders values using something like NSUserDefaults. Look it up for more info.
Related
My app contains TabBar controller with 5 viewcontrollers. It is possible to click on a button in each of viewcontrollers which will popup another view in which user can choose a setting. The button (which was clicked) is supposed to change its background according to chosen setting in each viewcontroller. So if the user clicks on button in VC1 and chooses the setting, this information should spread into all of the other VCs so that the button has the same background.
I am using storyboards, and I know that this is easily possible between 2VCs using segues, protocols, closures... I cannot find a proper way to spread information to more than 2VCs.
The only solution I can think of is usage of UserDefaults. I would save an information about a button setting and then call ViewWillAppear in each VC, where the background of the button would be set according to the value in UserDefaults. Is there a better solution, please?
EDIT:
As #cora mentioned in the comments, I was able to solve this using Notification Center.
You have several options, including:
Pass an array of the tab controllers to your "popup settings" controller and call a "settingSelected" func in each one directly.
Using Protocol / Delegate pattern, you could create an array of delegates in your "popup settings" controller.
You can use Notification Center.
You could subclass the button and use UIAppearance proxy.
Which approach to use will depend on a number of factors, based on exactly what all you need to do (are there other "settings"? or only that button background?)
You may want to search for swift using themes to see various different approaches.
“Is there a better solution, please?”
Not necessarily. Is this in fact a user default, to be preserved between launches? Then this is exactly what user defaults are for.
If not, then at least you need some central location where information about the current button color can be stored. An obvious candidate here is the tab bar controller itself. It gets notified every time there is a tab bar item switch, so it’s a perfect candidate.
Since you have mentioned you are using a UITabbarController, you can use an instance of UITabbarController and then access .viewControllers property of it and call their added public methods to get them triggered on events that you add. Additionally, using Notification Center makes more sense to me since your code will be more readable. Sometimes Notifications can just get a little confusing for new developers who are working on your code.
I have quite a few labels and buttons on my view controller and I have an image and I have set it to the back in the layers part on the left. How ever when I make it bigger it seems to go to the front and block the images. I have tried multiple codes from other people who have the same problem, but it still doesn't work. I am using Xcode 8, Swift, iOS.
In interface builder you can change the order of what is in front or behind by changing the order of the views as they appear in the Document Outline...
The higher up the list they appear, the further towards the back they are.
After a navigation has started, user has an option to switch between screens.
There are two screens with two different maps - one showing navigation and another showing some POIs.
Whenever screen is changed, new delegates are set and [SKRoutingService sharedInstance].mapView is set to the map view of that screen.
Everything works fine, only thing that happens and I want to avoid it - when I go back to the initial screen, the navigation starts from the start again (I was testing this in simulation mode on an iPhone 6 so far).
This happens when I set delegate
[SKRoutingService sharedInstance].routingDelegate = self;
If I don't set the delegate, on return to the main navigation screen, navigation will continue from the position it is intended to, but all the navigation delegate methods will not function.
However, if i set this delegate, navigation will start from the starting point.
How to avoid this ?
Currently, the "navigation" part is shared between all instances of the map - if you start the navigation in one instance, one you switch to another instance then you'll still have the navigation perspective. If you stop it in one view, it will stop across all instances.
What you could do is switch the map do 2D and enable panning - this way you'll still be able to interact with the map (zoom to your POI) and still have the navigation on.
Something similar to what happens in the demo project in the "Car navigation UI" demo when you start panning the map (after starting navigation).
You could really ask this question better. It's hard to understand what the question is about on first reading.
Having said that, it sounds like you're using a 3rd party routing framework, and setting that routingDelegate property has an unintended side-effect (of restarting the navigation). To avoid this, I'd create a new object that should be the routingDelegate all the time. Then this object can notify any other objects that need to know about routing events. This way, you avoid the side effects that are triggered by setting the routingDelegate.
I have a UIView that can be translated along the X-axis. Everything works great but I noticed that if I navigate to another page and back, the new view position is lost and reset to how it's configured in the xib.
In iOS8, it actually works exactly as I would want; the view persists its last position. However in iOS7, this doesn't appear to be true so I'm guessing Apple fixed it in their latest os? Is there a workaround anyone knows of that can get this to work in iOS 7?
Try storing the last location and when you pop up your controller set the view's x-axis to this stored location
I'm working on a storyboard-based project for ios5, and it is working well so far, but I want to save the user's state when they exit so that when they return to the app, I can jump them back to the place in the storyboard where they were when they left.
My understanding is that as you navigate through a storyboard, it creates a stack of UIViewControllers, so that when you dismiss the current controller, it knows which one to display. But if I jump to one of the UIViewControllers in a storyboard programmatically when the app starts up, it would not have that history, that stack of controllers, to backtrack along. If nothing else, if two controllers segue into the same controller at some point in the storyboard, it wouldn't know which to go back to.
What is the proper way of dealing with this? Do I just go through the storyboard to the point where the user left off, presenting each UIViewController with animations set to NO until I get to the one that the user was on? (And if so, what's the best way to store that information? Are there any methods/classes that can assist with this, or am I rolling my own way of storing their through the storyboard?)
One option is using NSUserDefaults. You can't exactly save the exact instances of UIViewControllers on the disk, and retrieve them as if they were in the memory. What happens is that, you should somehow, e.g. key/value, identify each of your viewControllers that you are interested and want to save their state. Also you need to key each element you want to save its state, and then save using NSUserDefaults.
For exmaple, let's say you have a hierarchy like this:
HomeView > ListOfNotes > NoteAtIndexPath:x
And the user is viewing NoteAtIndexPath:x, and also let's assume the font size is dynamic, so user can make it bigger or smaller at the time of viewing. You can define two keys: IndexOfLastNoteViewed and LasFontSizeViewed, and save them:
[[NSUserDefaults standardUser] setInteger:5 forKey:IndexOfLastNoteViewed];
[[NSUserDefaults standardUser] setFloat:17.0 forKey:LasFontSizeViewed];
When the application launches again, you can check the keys, and browse programmatically to HomeView > ListOfNotes > NoteAtIndexPath:5.
Here are some links that might help you:
Application states
App States and Multitasking
Beginning IPhone Development: Exploring the IPhone SDK By Dave Mark, Jeff LaMarche
-- Application Settings and User Defaults