iOS 10 go back to root in nav stack with parameters - swift

I have an initial ViewController that is a TableView with lots of data. I have a title bar button item that links to another TableView with a summary of the data in the first ViewController. I currently have it so that when you click on a cell, it redirects the user back to the first ViewController and scrolls them to the proper cells. However, it adds the view to the stack and doesn't actually move them back properly. I think I would be able to do this through Unwind segues, but I can't seem to get them to work through cell clicks. Is there a better way that I can achieve this?
When I switch to the other view, I also need to set two variables in the initial controller to tell it what index to scroll to.

1.UNWind Segue :
suppose that we need to programmatically trigger the backward navigation, based on an interaction with something other than the default “back” button on the navigation bar. How would you do it? Yep – you’ve got it: by using an unwind segue.
2.Other way is usual : PopViewController Method
As per your question , you can go with option 2 which is more easy .
As far as Sending data from SecondViewController to FirstViewController you can use delegate pattern to send data back.
Here is few useful post : https://stackoverflow.com/a/25523091/3400991
Feel free to comment. Thanks

Related

Segue moving too quickly while passing data?

I am making an app where the user inputs some numbers into a text field and it shoots out an answer on a label. I have the answer being displayed on another view controller. Using the same prepare for segue methods on all of the screens that I have only some of them work. The others will pass whatever text I have in the first view controller label (I have checked this by inputting all sorts of stuff and it shows up on the other side)... I have also deleted the segue and made sure that the calculation works and the label in the first view controller is getting the information.
This leads me to believe that the way it is set up is fine, but the segue happens so fast that the label on the first view controller will still have the original text when the segue is performed, sending that information over as a string instead of the answer...
Is it possible to change this? It would be the only logical thing that makes sense to me write at the moment. If it is possible how could this be fixed?
Any help would be appreciated. Thanks
EDIT:
This is the whole code I was Asked To post to help figure this out
So if in the first view controller I put a '?' for the text in the label just to hold the label, and in the second view controller I put a '!' in the answer label to hold the label.. The second view controller after I run the simulator the second view controller will show a ? the text from the 1st label... so it does show that it is pushing the data over, it is just not pushing the information in the label.
I think you have a made a segue to another view controller from the same button , with which you are performing the #IBAction func usednroicalc(sender: AnyObject), what you have to do is make a segue from view controller to another view controller in storyboard , give your segue an identifier and inside the #IBAction write at the place where you want to perform the segue , that is after your calculations write , self.performseguewithIdentifier("your identifier") . you can give identifier to segue by clicking on it and than find the identifier label in the inspector.
EDIT:
Here I have dragged from ViewController the one Highlighted as yellow in ViewController Scene to TableViewController the one above first responder in TableView Controller Scene.
Just wanted to answer my own question as I have found a way to push the info over finally...
You have to set up the calculations of the code in the prepare for segue and not in the button action. The button action is now only setup for the segue to the new view controller. not the way I wanted everything thing to go but it works.
Thanks everyone for the feedback... hope this helps someone else in the future.

Navigation Controller design for multiple duplicate views

I'm having a hard time understanding what approach I should take. I have a TableView controller with a list of questions, if you click a row it pushes a new QuestionViewVontroller that displays the question, answers, and next button (or done button). When a user clicks next I want to load the next question (from the tableview list) but I still want the navigation to work (back button will take you to TableView).
I'm starting to think I should keep the same QuestionViewController and just load in the question data. Is this right? Or should I use a modal view?
You have a couple of options here.
Use the same QuestionViewController and just repopulate it, like you mentioned.
Push a new QuestionViewController and use custom back button that either pops to the root view controller or pops to a given view controller.
The code for both options in #2 is below, where "self" is the current view controller (your question controller).
[self.navigationController popToRootViewControllerAnimated:YES]; //this would pop to your UITableViewController, assuming it was the root
[self.navigationController popToViewController:yourTableViewControllerHere Animated:YES]; //this would work as long as you passed a reference to your UITableViewController to your question controllers
It's up to you how you choose to solve your issue. However, if you decide to use the same QuestionViewController, include a catchy animation when importing the new question so the user knows what is happening and to make your app that much cooler :)

Managing navigation views on the stack

Being new to iPhone development I am really struggling with this concept. I have built an App which is not quite running the way that it should. I have a TabBar app. The secondViewController is essentially a form which I use to collect information about daily exercise. I have embedded this secondViewController into a Navigation controller so that I can push to a datePicker view and return with the date. I also push to a pickerView for exercise type and return with data. At least this is the intended process. I use the prepareForSegue to push the picker views and return to the secondViewController carrying data between them with each segue.
What I find is happening is that instead of pushing to the picker views and returning to the secondViewController my app seems to progress in a linear fashion. To be more precise it seems to push to the date picker, then push to a new 'instance' of the secondViewController, then push to pickerView, and then to another new instance of secondViewController. This means that when my users eventually touch the save button, they have to use the back button in the navBar to get back to the original secondViewController.
[Have an image to add but can't post it as I have reputation under 10 :-( ]
I have read Apple's documentation on managing the stack and hierarchy, but I just seem to have confused myself more than anything else. I have also searched for answers and tutorials, but I am either missing something or there is no clear explanation of how this is supposed to work that I can find. Can anyone tell me how to push to a pickerView and then return, or at least to return programmatically to the original secondViewController when the save button is finally touched?
To go Back in hierarchy in UINavigation Controller you can use
[self.navigationController popViewControllerAnimated:YES];

How to switch to a TableView from a first View?

I'm working on my first app and I've issues on how to layout some of its logics.
Basically, what the app is supposed to do is to show a first screen when launched where user can fill in some values and press a button that opens a tableview which shows results. The first screen (view), outlets and connections are all working fine. The issue I'm having is how to leave this "home" search view and show the results to the end user on a table view. Right now, I've only 1 view with its related View Controler and this controller handles the tasks of taking user inputs and get results throughout a HTTP post request.
I need your guidance...Thx in advance
Stephane
Is there a reason that this all has to happen on one screen? iOS is set up to allow for this to happen very easily and (I think) attractively by using a UINavigationController and pushing in a new view controller (could be a UITableViewController or simply a UIViewController that contains a UITableView).
If you MUST have all of this take place in one view, Swastik is correct that it will require some view acrobatics. One way to do it attractively is to use the UIView animations introduced with iOS 4.
Here's Apple's UIView documentation: UIView Class Reference
Specifically, check out the CLASS methods of:
1. animateWithDuration:animations: (with or without completion:)
2. transitionWithView:duration:options:animations:completion:
Note that these methods will require you to learn a little bit about blocks, but it's worth it and I think blocks give tremendous power!
With the methods mentioned above, you could fade out your form and fade in your tableview, or maybe just move the form off-screen while the table view flies in. The possibilities are limited only by your imagination.
u can add a table in ur xib.Initially make it hidden, & when u need to show it unhide it & also if u want to update table's data , you can reload the data of the table.

Pushing a new viewcontroller via a next button?

Sorry I can't show code for this question - it should be fairly simple to explain..
I have an array of data (object1, object2, object3, etc)… I have one tableview that lists these objects (object1.title, object2.title, etc) and upon clicking it push's a viewcontroller that shows the detail of each object. Now, rather than have to press Back on each detail view, I'd like to put a next button on the detail page..
I could easily push a new controller, however it would end with a chain of:
List View -> Detailview1 -> Detailview2, etc..
which would be a pain to traverse back up the stack.. Can the parent view controller be removed / changed easily or is this not the best way to do it?
Cheers.
You can always pop back to a specific index in the stack. So you could have a back button that pops back to the parent. If there is no real reason that the user would need to go back through the detail view controllers you could swap controllers by replacing the stack with a new stack using setViewControllers:animated: Look through the UINavigationController documentation and see whats possible.
This code will "pop" the view contoller off the view, so it removes it, instead of overlaying another view on top of it. Add this to your button action
[self.navigationController popViewControllerAnimated:YES];