UIPageViewController reset states swift - swift

i would like to ask if there is any ways we can reset our view in UIPageViewController implementation. For example I have two views,
in View A, there is a button which changed text upon clicking as well as a tableview. Once i have clicked the button or when i have scrolled to the middle in my tableview, when i have swiped to View B, my states in View A will not be reset when I go back to View A where the button's text are not reset and my tableview is in the middle where from where I scrolled earlier.
Is there any ways once we move pages between UIPageViewController, the pages reset their states?

The answer is "It's kind of complicated."
The background:
If you only have 2 pages in your page view controller the page view controller will probably keep strong references to both, and so they will retain their state as you page back and forth between them.
If you have more than 2 pages, and are not showing 2 pages at the same time and using a center spine, the pages that are not either currently visible or next to be displayed will likely get released and you will then recreate them when the user displays them. In that case the pages won't remember their state unless you do extra work so that they save their state when they are moved off-screen.
Your case:
You always want your pages to reset when the user swipes to a different page and then back again.
I'd suggest you write your pages' view controllers logic so they respond to viewWillAppear(animated:) by resetting themselves to their starting state. (scrolling table views to the top, resetting button states, or whatever.)
Ideally, you might want to make that an option that is controlled by a flag (resetsOnAppear: Bool). That way if you later decide you don't want that to happen, you can just set the flag to false and don't have to refactor your code.

Related

Data Retention on VIewController/Views

Had a quick question:
I have 2 view controllers, and their associated views.
The first VC have images which have been coloured through masking. This is a link to the technique I will be employing.
The second VC and view is simply a settings screen that will hold random content.
Assume this scenario:
First VC with images is loaded up and the coloring is completed
NOTE: Coloring is random and the value is not stored anywhere. With
so many images it would be difficult to store all those random
colors in variables.
On the First VC the user presses the button navigating to the
setting screen
the Second VC is now loaded, via a Modal operation
On the Second VC the user presses a "back" button to return to
the previous VC, which in this case is the First VC.
The question: During navigation and after the the navigation back to the First VC will the images and colors I randomly and programmatically chose still be visible or will I lose those randomly generated colors? If not how do I ensure that what has been rendered on the First VC will be maintained throughout any navigation operations?
As long as the View Controller stays on the navigation stack, all of the data it holds will still be valid when you return to it. Pushing any number of View Controllers on top of the stack will do nothing to the first view controller, but as soon as you pop the first one off of the stack, it will lose any data it had unless saved otherwise and reloaded(in a variable in the App Delegate, for example).
You will not loose the images you created unless you release the first view. Since you are showing the second view on top of first view, all the images will be there. You dont have to worry about saving the colors or images. You can push any number of view controllers on top of this first view controller.
Note that this is valid only if you are not removing/popping/dismissing the first view controller or its view.

Multiple View Controllers being loaded at the same time. iPhone app

I am trying to create an app that swipes over through multiple view controllers on a UIScrollView - similar to how one would see different windows in the safari app, but instead of tapping a button to move between them, I am swiping the scroll view.
Now, I will be getting notifications when any of the data in a particular view is to be updated with some json. Should I be updating the view that aren't showing (but are on the scroll view), or should I wait until the user scrolls to that view?
I am very concerned about performance here. Hopefully I am being clear in the question.
Thanks!
You should update the views next to the view that you are viewing I would say. And when you switch (scroll) to another view, then update the views next to that view. (Assuming you have received new JSON data)

UINavigationController skipping a pop

I have 3 view controllers, one is the root, which pushes the next, which pushes the next. Each time you can go back (pop), using the normal back button that appears by default.
However when the 3rd view is visible, when the user taps the back button I need it to skip out the the 2nd view controller, and go (pop) directly to the root view controller.
How can I override the default back button behaviour? (I'd like to keep the shape of the back button, and not replace it with a square bar button)
You could try to have an object implement UINavigationControllerDelegate and enforce this behavior. You could also have a designer create the same size and shape asset and you could use this as the background for a button.
However, I'd rethink your UI if you really need to do this. It is contrary to the user's expectations of how a navigation controller should work.

UINavigationController reloading UITableView

In my application I am parsing XML to create a UITableView. When a user selects a row it changes the file that it is parsing and reloads the UITableView.
I want to create a back button so that the user can go back the the previous XML page they were viewing.
In short how can I tell the navigation controller to create a back arrow for this page when all i am doing is reloading my UITableView?
I'd strongly suggest building another controller (i.e. UITableViewController) and push that instead of just reloading the table. This makes the button automagically, and (major plus here), it animates the whole digging down / stepping back in a way that the user is expecting it.
As a side note, I didn't manage to get a back-style button once I tried it, just a plain normal button (make a new button and set it at the navigation bar).
What you're describing is a navigation. Rather than reloading the table's data, simply push a new view controller onto the navigation stack every time a row is tapped. This is a fundamental pattern in iPhone development. Here is Apple sample code for a multi-level drill down table view.

iphone persistent button on all views

I have a navigation app that has many screens the user navigates to. A handful of views manages these screens dynamically. What I want to try to do is add a button that will always show up on every screen the user views. I need to do this so that the user is always able to preform the action associated with the button regardless of where they are in the app.
Is it possible to achieve this by adding this button only once and having it passed between views like my navigation bar is? Or do I just have to man up and add this button and its functionality to every single view file I have?
Thanks
I would say it probably depends on what the button does. If the button is generic to all views, meaning it affects all views the same exact way so no customization for a given view is needed, then a way to do this would be to include the function in the App Delegate or as a subclass to your Navigation controller.
You can then use the rightBarButtonItem to always show the same button and just access that method. You would just have to add code for the rightBarButtonItem in each viewDidLoad (but they'd all be the same).
I did something similar to this with an "Upgrade" button on one project. Since all the button does is launch the AppStore to the paid version, it's independent of all views and I can place it anywhere.
You can put the button on the navigation bar if you want. Alternately, the more generic way to do this would be to split your single view into two views. One is small and only contains your button but always stays on the screen. The second is your workspace and you swap in and out the views that are displaying the current content. You'll note that this is the way the navigation controls and tab-bar controls work.
The last way to do this would be to put different buttons, in the same place, on each view and have them all trigger the same action. As far as the user is concerned this looks like the same button. Disadvantage here is that you can't alter the button across all views in a simple manner.