In my app I have a UINavigationController that puts a navigation bar on all of my ViewControllers and in every ViewController I have a custom back button. In one specific ViewController I need to send some data when going back to the previous ViewController. I was able to do this when the user presses the back button with a custom function, but this function is ignored when user swipes to go back.
I tried using this answer: Adding an action to back swipes swift 4 but calling visibleViewController or topViewController in the navigation controller only gets the view controller that we're going to (the previous ViewController, not the current one). I need the function inside the ViewController we're in before the user swipes back.
How can I call on the back function I made inside my current ViewController to send the data from that ViewController to the ViewController we'll pop to.
Seems like you just need to use viewWillDisappear(_:) and just put the function you made here.
Related
I am performing a segue to open another ViewController, afterwards I go back to the initial ViewController also via performSegue.
If I am doing this, all the values in the initial ViewController (e.g. the image in the UIImageView) are destroyed.
Do I need to save everything before I perform a Segue, or am I doing something completely wrong?
Thanks!
EDIT:
This is what I would like to achieve, if I press the menu button on my "HomeVC", the tableView of the SWRevealViewController pops out from the left side. If I press a cell, I get with segue push, to another UINavigationController. From this Controller I would like to go back to the "HomeVC".
Here is a screenshot.
You do it wrong. Performing a segue will create a brand new controller from the segue's destination and present/show it.
If you want to go back, you shouldn't perform another segue. Instead, you must dismiss() it if you presented the newController modally or pop() it if you show/push it.
This tutorial may help
So, I have a tab bar view controller, however at some point I need to manually segue to a viewcontroller that is not attached to the tab-view controller. The segue works, but then it jumps back again to the previous view controller randomly after 1/2 a second to 3 seconds, not all the time, but often. Occasionally I see a message saying something along the lines of "cannot dismiss viewcontroller while another dismiss or presentation is already taking place" but I've double checked all my code and there are only three places that make the jump, none of which can be called simultaneously. What I need to know then, is how I go about dismissing the tab viewcontroller in the view did load method of the modaled to controller. Swift explanations are no good to me thanks. Also, not related to this question persay, but can anyone tell me what the sliding bar thing that looks like a UISlider and tracks the progress of a song is called?
I'm basically just asking, is there a difference in how the code/app is running between these two scenarios:
1) You push to a viewcontroller
2) You push to a viewcontroller, then pop that viewcontroller, and then push to it again
It seems like there must be, because my app works correctly in situation 1, but looses some functionality in situation 2.
I have a navigation controller with push segues between viewcontrollers. In the main menu screen you can press a button which is a push segue to another viewcontroller, viewController2, which shows an imageview with a map on it, and then the user chooses a location and the imageview displays directional arrows.
There is a back button viewController2, which is just a standard custom back button:
- (IBAction)customBackButton:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
If I click this back button, I get brought back to the main menu... and then if I then navigate to viewcontroller2 again, with the same push segue, then some of the functionality doesn't work anymore. It shows the map, but the arrows won't get put onto the map.
It seems like the code can't interact with some of the properties of the viewcontroller2 class. For instance, I have a UILabel called _destinationLabel with text that should change based on the location the user chooses. This never gets changed, even though it goes through the code correctly. However, other labels do get changed correctly.
I know that a push segue automatically adds a back button so that the presentee can return to the presenter on click. What I want is for the back button to be available on the first UIViewController of the NavigationController (basically the only view controller in the chain that does not have a back button). How do I force add the back button?
I cannot simply add a custom image because I want the exact same chevron that comes with the iOS back button.
The easiest (yet hackish) way to achieve this is probably adding a dummy view controller in the stack before the actual first one, so you will get a proper back button. Use dummy's backBarButtonItem to customize the button title if you need, and -viewWillAppear: to respond to the button being tapped.
What I want is for the back button to be available on the first UIViewController .... How do I force add the back button
This whole thinking and logic is wrong. This is not how iOS UINavigationControllers work. You have a starting page (mainViewController) in a UINavigationControllers and from there you "move on" to something else. But you always come back to the main view controller. Just think about it, if you put a back button on the main view controller, where will the user go back to?
If you have a situation like this.
SomeViewController --> UINavigationController --> MainViewController
Once user is on SomeViewController and you take him to MainViewController which is part of UINavigationController then back button makes sense.
Just a FYI - you can easily drag and drop a UIBarButtonItem to any UINavigationController in Xcode and call it "Back". You will have to then connect that back button to another view controller.
Maybe it doesn't make sense but why can't you add a custom image? If all you want is "Back" chevron, can't you take a screenshot of this button and then put this image on your custom UIBarButtonItem?
For my iOS application, I have a login screen and a register screen, each with their own UIViewController (LoginController and RegisterController respectively). On the register screen, I have added a back button that performs a segue to the login screen.
When I click the back button, the implementation file seems to change (from RegisterController.m to LoginController.m), but the actual screen does not change. I have added the code NSLog(#"In LoginController"); at the top of my LoginController.m file (after [super viewDidLoad];), and when I click the back button on the register screen, my log prints "In LoginController" every time I click the back button, but the screen never changes.
I created the segue similarly to other segues in my application that work correctly. I used the storyboard, held control on the button and dragged the segue to the LoginController.
Does anyone know why this is happening?
EDIT: So I have changed my segues to unwind segues, so that I can get back to the original view of the navigation controller. I have an unwind segue from LoginController to TutorialController, but when I try to unwind from RegisterController to LoginController, the view does not change (but the unwind method in LoginController is called).
So I figured out the problem.
In performSegueWithIdentifier, I had sender:self instead of sender:self.backBtn