With a navigation controller in Swift/Xcode, can I create an unwind segue from a View Controller to the preceding one without creating a custom UIBarButton?
Basically, if I replace the "back" button that's created by default on the navigation item, I could link that to "Exit" and create an unwind segue. However, I wanted to see if there's a way to use the existing "back" button and link a segue identifier to it.
(The one thing I've found out from research which is useful is the default "back" button actually belongs to the preceding View Controller, but this doesn't help me solve me problem.)
I found an answer here closely related to my question:
Unwind segue with Navigation back button
Unfortunately I didn't find this in my initial research and only stumbled upon it in the "related" section after I posted my question.
If you don’t want to insert new UIBarButton then you will most probably end up tracking this event with viewWillDisappear or didMoveToParentViewController method.
Here’s the same question, you might find it helpful:
How to trap the back button event
Related
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
I'm new in Swift and iOS dev however I think that I know how to eat it.
I have a problem when trying to use popToViewController. The problem is that I want to "clear" UINavigation stack when pressing the button.
So I did this like here:
navigationController?.popToViewController((navigationController?.viewControllers[0])!, animated: false)
I want to have only first ViewController in the stack.
Before I click on the button stack contains
([<EmpMan.ViewController: 0x7fd191535ba0>,
<EmpMan.TableViewController: 0x7fd191564be0>,
<EmpMan.NewEmployee: 0x7fd1916c0320>])
When I click on the button the stack looks:
([<EmpMan.ViewController: 0x7fd191535ba0>,
<EmpMan.TableViewController: 0x7fd191564be0>,
<EmpMan.NewEmployee: 0x7fd1916c0320>,
<EmpMan.TableViewController: 0x7fd19171dbf0>])
I think after popToViewController the stack should contains only ViewController. Why it does not work?
If what the button does is a segue, than just do the "Show detail segue and you are done.
Otherwise, make a boolean in the viewController with the button that is set to true when you press the button. Create a secondary segue out of each of the possible segue routes out of the controller. Then if that boolean is set to true, use a "Show Detail Segue" as your next segue instead of a "Show Segue".
Show Detail automatically clears the navigation stack. There are other ways of handling this as well, but I am just throwing you an acceptable answer that works. Read more about segues and custom segues and the navigation stack to get a thourough understanding. It is worth your time if you want to get heavy into iPhone App dev.
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.
I'm currently developing a framework in Swift that includes a storyboard that I designed myself. What I need to do is that the user that uses my framework can create a segue to my storyboard. Then I'd like to be able to recreate a segue to the user storyboard that that I "leave" my storyboard.
How could I do so ?
Thx !
Your question is vague and I am not entirely sure what you are asking. However, I can't comment yet so I will try to answer with what little I know. It seems like you are asking how to transition storyboards. The simplest way is using a UIButton in your storyboard. Open up storyboard and go to the navigation controller. Then, add a button to the Nav Controller. Right click (or control click, depending on your settings), and drag the line to the main storyboard. Lastly, click on the show option. When a user clicks on the button, it will immediately switch to the main view controller. Don't forget to add the button in the view controller. Hopefully, this is what you meant and helps. If you wish switch view controllers in code, this is a duplicate and you should go here: How to switch view controllers in swift?. If this doesn't answer your question, comment and I will try and help. For future reference, please provide more information: snippets of code, pictures, anything that better demonstrates your problem.
I fixed it by getting the instance of a new view controller added on the user storyboard with the view controller identifier like so :
ProcessOut.backViewController = storyboard?.instantiateViewControllerWithIdentifier("back")
And then I present it like I would present a normal viewController from my storyboard.
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 :)