iOS navigation best practice for adding items - ios

I cannot find the answer to this although I have implemented this rather a few times already, but maybe the wrong way.
Say I have an App for iOS, it has a main screen, which goes to a list, that list has a < back (to main) and an add button. Now when I click < back, I go back to main as that's the pop() from the stack. No issues so far.
Now when I click the add button, that is added to the stack as well; when I click back on that screen I go back to the list which is fine.
The problem is; when I save the new item, I want to go to the detail screen, but I don't actually want to have the add screen on the stack anymore while it will be there. I want the < back button for the detail item pointing to the list.
I know how to do this, but what is actually the best to implement this with the navigation stack?

Well for adding elements the best practice is to present an ModalViewController.
In this way it is not added to the stack.
Update
Let's take as examples simple apps that apple provide with iOS, Contacts app. When you want to add a new contact a VC is presented.
You'll need to implement "Done" or "Save" button that will dismiss the modalViewController and if you want to take the user into detail screen you could post a notification or other mechanism on dismissViewController method's completion block that will push the detail page from the list. But be careful on animations if you dismiss the modal VC animated and push the detail page animated you could get some unexpected behaviour. My proposal is to dismiss the Modal VC animated and push the detail page without animation.

You can override UINavigationController's viewControllers property after you pushed detail view controller. Just get current viewControllers property array, iterate and find the one you don't want to see and remove it. Remember to set viewControllers array again.
https://developer.apple.com/library/ios/documentation/Uikit/reference/UINavigationController_Class/index.html#//apple_ref/occ/instp/UINavigationController/viewControllers

Related

Changed segue animation to "Show" but it's stuck in "Modal"

After creating ViewController informational pages which use UISwipeGesture to swipe left and right between 4 different UIViewControllers, I first selected "Modal" as the segue animation. But modal animation is contrary to the left/right swipes which should have "Show" segues which better match the user action. When changing the segue types in Interface Builder to "Show" the animations did not change. When de-selecting the "Animates" checkbox, XCode WILL eliminate the animation entirely, but then when selecting "Show" it goes right back to a modal segue animation.
Would show a picture of the set-up, but don't know how in StackOverflow
Tried embedding whole stack into a NavigationController and that didn't work.
Tried deleting segue and reforming it as "Show" from the getgo, that didn't work.
Shut down XCode and Simulator and restarted-- that didn't work.
Yet in another part of the same storyboard, "Show" segue works fine.
The problem was my segues were selected with Show Detail (replace) instead of Show (eg Push). When in interface builder you'll see you get presented with both of those as the top two selections. When using a UINavigationController, it expects Show (eg Push) to be selected which will put a Navigation header at the top of your UIViewControllers with a back button. In the case above, I'll simply write a short amount of code to hide the nav-header at the top of all my informational pages. I will keep the UIGestureControl left/right swipe code as a novelty item, but there are other ways to allow the user to swipe left and right to new pages (lookup PageViewControllers).
UINavigationControllers and Show (eg Push) animation go hand in hand, and you have to be wary of Apple UX/UI guidelines which protect the sanity of users before you go getting experimental with animations in segues.
Show (eg Push) arranges previous, current, and future ViewControllers like a stack of pancakes, so when you move from one to another you're pushing the top off like the top card of a deck of cards. Whereas Show Detail (Replace) acts like you're removing the card and replacing it with the next card. Best way to think of the difference between those two segues.
Not sure why compiler kept the modal segue after I changed it to Show Detail (replace)-- could be a bug with UINavigationControllers?

iOS Push and Pop view controller at the same time

I am currently working on an Xamarin iOS application that has some navigation issues.
What i am currently trying to achieve is to push a ViewController while at the same time popping the previous one off. The reason for this is that I have one ViewController that shows some specific questions from this view controller i can spin up another ViewController of the same type and the user can fill in some more questions. What i want to be able to do is pop the previous filled in view controller so when the user hits the back navigation it takes me back to my home view controller (and not through all the previous instances of the question controller).
I also can't use the PopToRoot or PopToViewController.
I have tried doing a pop without animation and a push with animation and results in a quick flicker of the underneath viewController which is colourful so its a bit of a shock to the eyes.
The other thing I have tried is to manipulate the navigation stack underneath by pushing on the questionController and then removing from the stack the old viewController the problem with that is i have some tear down on the ViewDidDisappear (i work out if its a pop disappear rather than just a cover up) which doesnt seem to be called because of the manual removal. So along this method is it ok to call the ViewDidDisappear to carry out my unwiring so there is no leaks?
Many Thanks for your time reading.

How to fix segue problems between ViewControllers using swift?

I got 3 view controllers. On first page user defines some variables and when he executes it, it goes to second page with segue.identifier. On second page a time counter starts and an item price is decreasing over time. Whenever user uses buy button, the program prints on third page item number, updated time and price. Then if user uses show items button, he will go to the third page and see printed lines. However, I put a navigation bar with a back button on top of the third page, when I use back button, it crashes.
Here is my question: How can I fix it without changing updated price and time counter? With segue.identifier? If yes, how?
Here is what you have to do. Select the 2nd view controller, go to the edit from top and follow the path: edit -> embed in -> navigation controller.
This will automatically adds the navigation bars and back button to the 3rd controller.
Hope this helps .
Don't add your own UINavigationBar. Like Kutay Demireren said, you want to embed your first view controller in a UINavigationController, and then the segues will work by pushing each destination controller onto the navigation controller's stack.

conditional refreshing of view in tabBarController

i have implemented a tabbarcontroller with 5 tabs each connected to a view.
the tabBarcontroller is created and default view allocation happens in another view.
everythine looks fine but i have a bug.
each view in the tab have buttons that trigger other views. these views don't have tabs so to get back to a tab view i use a back button.
when i press the back button i don't want the view ( with the tab ) to be created from scratch. so i have put the relevant code in viewdidload rather than viewwillappear.
( this choice is because this view downloads data from server and it becomes time consuming to put the code at this point in viewwillappear )
However if i am returning from current tab to a tab that i had previously touched and viewed. I want view to be loaded from scratch because data should be downloaded again at this point. but since i am using viewdidload rather than viewwillappear the old view is not refreshed.
how can i achieve this conditional refreshing of view depending upon whether i am coming from another tab or a from a view with back button
One way to do this is the isMovingToParentViewController method in your view controller. This will enable you to detect if your current stack has been popped from navigation controller.
There's an answer here that explains how to use it, and links to Apple's documentation.
As you can see from that thread, you can also use NSNotificationCenter to do this.

Use case for push versus modal segues?

Let's say, I have a scene (pushed view controller with a navigation bar), which displays some tabular data in a table view.
In the navigation bar of that scene I have a + sign, which should open a new scene, where the user can add a new item (row to a core data table).
In the table view, each row has an arrow on the right side of each cell, which opens a scene where the user can edit that particular item's details.
Should I use a push or modal segue for the +?
Should I use a push or modal segue for the arrow?
What is the "best practise"?
I understand the difference between push and modal segues, but I want to know which is better suited for the above use cases.
If you want to follow Apple's best practices, I would suggest the following :
For the "Add" functionality, use a modal segue.
For example look at the contacts app. Pressing + shows a modal view controller.
What's the logic ? for start, modal view controllers usually have a "cancel" button, as opposed to the "back" button on a pushed vc.
When the user presses "back" - he'd expect a way to come back to the vc. Usually "back" saves your data on iOS (auto-saved).
So by using a modal segue you force the user to submit the form , or cancel. The modal presentation hints that you really need to fill this screen.
For editing - push. but modal could work as well (and you could reuse the same VC).
Reasons for push :
you get a hierarchy of vc's , going back and forward while drilling down.
(you should implement) auto saving when going back (just like other iOS apps)
For adding a new entity to the core data table, on tapping the + button (I assume its a right bar bar button item on the navigation bar), use the modal segue.
The view for adding a new row for the enity has to be presented modally and once the save is completed, dismiss the modal view and reload the table view to display the newly added item.
Also for displaying the details of an entity row, use the push segue. A user expects a push action when he selects a table cell and it is the ideal way to do that.
I hope this quick summary will help you :
When you want to show a detail view of a summary view, use a navigation controller and Push Segues. If the "parent" view doesn't really relate as far as data is concerned to the "child" view, then use a modal. A good example for a modal view would be any entry view. This view doesn't really have any relationship as far as data is concerned to the "parent" view., the entry screen will just take data dat from user & will save & can go away & giving control back to parent

Resources