I'm trying to call function when i'm pressing default Back button (e.g. < Settings) in Navigation Controller.
At other situations i'm creating custom button, after i'm Ctrl+Dragging to red Exit button in XCode and pointing it to Selection segue.
Now i need to use default back button of navigation controller, but i can't set assign it to segue action because it's no back button in Storyboard view (but it exists in Simulator).
Simulator:
XCode:
I can create custom button in navigation contoller and assign it so custom unwind segue, but that button will be ugly without "< " symbol.
I've found some answers at stackoverflow, but they all are written in Objective-C :(.
You can use a delegate method to accomplish what you are doing.
Here is a really thought out swift tutorial that deal with delegates and segues:
http://makeapppie.com/2014/07/05/using-delegates-and-segues-part-2-the-pizza-demo-app/
What you are asking, ability to catch the back button's action, isn't possible the way you describe it.
Most people would recommend creating a custom button, then use an unwind segue.
There are lots of tutorials on custom back buttons just in case you're new to swift, and the ugly part of the custom back button can be easily fixed by putting your own arrow there.
Also I found this previous asked question for future reference: Unwind segue from navigation back button in Swift
Actually I just assigned the unwind segue to the entire view controller (right click from the yellow view controller outlet and drag to the exit outlet then select the function). And it works great so far. No need custom button, no delegate.
In fact, in my project the child is supposed to call the different parent's function according to current caller view controller and simply using the same function name made my dreams come true.
This page helped me a lot: http://spin.atomicobject.com/2014/12/01/program-ios-unwind-segue/
Related
i'm doing an app that uses a TableViewController with a system of Adding/Editing items, using the same view. (however when you add an item the view is modal presented and when you edit it is shown)
I followed the great starter tutorial from Apple so basically my Storyboard looks like this
The segue going through the Navigation Controller is for adding and the other one is for editing. (I did everything according to the tutorial).
I did a segue between the Cancel item bar button and the exit icon of the ViewController and it works well when the view is modally presented (when I try to add an item).
However when I click on a cell to reach the view with the segue that shows it (to edit an item), both items in the navigation bar stop working. The prepareForSegue method is not called anymore. So I can't cancel or save.
I tried creating an unwind segue between the ViewController itself and the exit icon and to call it programmatically like this:
#IBAction func testButton(sender: UIBarButtonItem) {
print("we're inside")
self.performSegueWithIdentifier("cancelSegue", sender: self)
print("so what now")
}
and when I try to edit it and tapping the cancel button it results by just showing the two log messages and kind of skipping the performSegueWithIdentifier method. However adding still works fine.
Am I doing something wrong or have I misunderstood some basic notion about unwind segues?
This seems like quite a strange solution to your problem. While I can't comment on specifically why your unwind segue isn't working in the second case, things will start to get quite complicated with dismissing the New Programsegue since you're displaying it two different ways.
The common approach that we use is:
Use two completely different view controllers for creating and updating an assets. May be some duplicate code, but makes it slightly easier for other people to work on.
Use the same view controller for creating and updating buttons. If you're editing an item, you can pass it to the view controller with prepareForSegue. When the view controller loads, if an item is present, you can change the behaviour of the buttons, title etc. If no item is present, you know to create a new item.
For most implementations, it's much simpler to dismiss the views programatically. (i.e [self dismissViewControllerAnimated:NO completion:nil] for modal views and [self.navigationController popViewControllerAnimated:YES] for just returning to a vc in the same navigation controller stack.
Update
Did you link the Cancel button to the exit icon on the first view controller, or the second view controller (the one with the cancel and save button)?
I just started with Xcode and Swift.
I try to build my first little App for iOS. But now I have the problem, that I don't know how to implement a the back button, so that i come back to the view before.
My Storyboard look like this:
When I open the A-Z view, I want to display the Back Arrow, which turn me back to the Item 2 view.
To open the A - Z view I connect the button "Medikamente A - Z" with the Navigation Controller.
When using storyboards the back button is usually implemented with unwind segue.
I usually like to follow raywenderlich toturials on UI related topics, like this - http://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2
It include a detailed example of how to implement back button in storyboards. Quoting from it -
Storyboards provide the ability to ‘go back’ with something called an unwind segue, which you’ll implement next.
There are three main steps:
1. Create an object for the user to select, usually a button.
2. Create an unwind method in the controller that you want to return to.
3. Hook up the method and the object in the storyboard.
When using UINavigationController, whenever you push to a new ViewController the back button will automatically appear, so you can jump back to the previous View Controller.
So it's works like:
UIViewController -> UIViewController -> UIViewController
A back button will appear on the last 2 so you can pop back the the previous ViewController.
You don't have to do any additional coding for the back button to appear, it'll do it on its own. I hope this clears it up. Let me know if you have any questions.
To implement a back button, your root view controller has to be a Navigation Controller.
The first view controller becomes the navigation root of the navigation controller.
If you want to present another view controller, you select a "Show Detail" relationship as the action for the button which should show the view controller. To do this, Ctrl-click and drag from the button to the destination view controller and select "Show Detail".
I had the same problem, even when on the storyboard the back button was visible at design time.
I deleted the segue, and recreated it with "Show" instead of "Show detail". Changing the segue to "Show" had no effect. I think this is a bug, so if you miss that back button delete and recreate the segue.
I'm sure this is super simple but I'm new to iOS development so I must be missing something.
In xcode I have a like 5 page app and I can navigate forward no problem... To navigate backwards shouldn't I be able to create the button, then simply control and drag to the page I want it to go back to?
Yes. This is called an unwind segue. If using swift, this is how to do it.
Write this code in the view controller file that you want to go back to:
#IBAction func unwindToPreviousScreen(segue: UIStoryboardSegue) {
}
After writing this code, go to the storyboard, and go to view controller that is the one you don't want to go back to, and control-drag from the view controller to the exit icon in the tab bar of the view controller (far right icon). Make sure you have clicked on the view controller.
It doesn't have to be unwindToPreviousScreen, it can be whatever you like, but be sure to keep the unwindTo part intact.
Apologies if you are using a different language, I am just a swift person.
Up to now, there is a button in a view controller (let's call it Main View Controller). What I want is: if it satisfies a certain condition, when I press the button, the segue would lead to View Controller A; if not, when I press the button, it would lead to View Controller B.
But it seems that one button can only have one segue from it, so I wonder whether what I want is possible to be achieved in storyboard???
Thanks in advance!!!
If this question is too basic for you, I am so sorry. I am still a very starter in iOS programming.
You need to create a segue from the viewController itself to the destination. You can do this for each viewController and give them appropriate identifiers.
Then you hook up your button with an IBAction and decide which segue to perform
Custom transitions are fairly new to me. I had to incorporate them into the last project I worked on and ended up using both the UIViewControllerAnimatedTransitioning protocol and custom segues.
Custom segues seem a bit cleaner/friendly in that you can choose them in a storyboard and be done with it. However there doesn't seem to be as-friendly way to set up a back/pop segue. I've read about unwinding segues but I can't seem to find anything around tying one to the back button of a nav controller.
The UIViewControllerAnimatedTransitioning protocol approach has a bit more set up but allows you to specify both the entering and exit of the views.
In the case of my app not being able to pop back with my custom segues wasn't an issue because the flow of the app doesn't allow you to go back to the previous views. Most applications though require this thus a custom segue seems worthless unless you subclass UINavigationController and allow for custom popping segues.
Am I missing something because it seems UIViewControllerAnimatedTransitioning is the best approach to take for animations between view controllers. Why would I ever want to subclass UIStoryboardSegue?
I agree that there is a lack of documentation on unwind segues compared to other types of segues, but after you get the hang of them, they are pretty straightforward. My understanding is that segues (including unwind segues) is the way that Apple intends you to transition between view controllers. Even when you create custom segues, the regular unwind segues should still function.
In my own work, I have subclassed a UIStoryboardSegue to execute a custom animation when transitioning between segues. Using a widely known app as an example, when you tap the menu button in Uber's app, the map view controller moves down, and a table view controller appears. And when you a tap a row in the table, the new view controller slides in from the right, but the map view controller is still visible on the screen. And when you tap the map view controller, it returns to its original position. For some reason, I believe that Uber actually didn't implement segues at all, but just place view controllers on top of view controllers, but I have implemented something similar in my own app with custom segues. These segues are difficult to replicate with Apple's default segues, so I used custom ones.
If you are tying an unwind segue to a back button, then you will want to override the normal unwind that comes along with the default back button in the uinavigationcontroller. I have found it very difficult to customize that segue. I would recommend hiding the default back button that comes with the uinavigationcontroller, adding your own bar button, and tying this new button to an unwind segue. I know that this is annoying, considering the default back button has some added functionality, such as using the title from the previous view controller as its text when the title is short enough. Unfortunately though, I think Apple really wants to discourage you from customizing the default button and makes it difficult to alter. I have left out how to replace the default back button with the custom one, so let me know if you have trouble.
Anyway, to create the unwind segue, you must first create a method in the class (or parent class) of the uiviewcontroller you are unwinding to (not from).
- (IBAction) methodName:(UIStoryboardSegue *)segueName
You can change the methodName and segueName to whatever you like. Now go to your storyboard, and go to the scene containing the uiviewcontroller you are unwinding from. If you now ctrl drag from this uiviewcontroller (the yellow button on the left side of the bar at the top of the controller) to the exit button (the orange button on the right side of the same bar) you will now see a menu popping up that contains the methodName above. Click that method, and your unwind segue will now be created.
After you have created the unwind segue, you now see it in the outline on the left of the storyboard. If you click the segue, you can inspect it and give it an identifier.
Now to deal with tying it to the back button...
Again, viewing the uiviewcontroller you are unwinding from , if you control drag from the custom back button (not the default back button) in the storyboard to the class of this uiviewcontroller, an IBAction will be created for you tied to the button. In this method, add in:
[self performSegueWithIdentifier: segueIdentifier sender:nil];
where segueIdentiferis the identifier you gave the unwind segue above. Now when you tap the back button, the unwind segue will be executed. You can also do some animations or what not before the unwind segue is executed.
I have actually done some complicated custom unwind segues dealing with animating both the source and destination view controllers. If you can be more specific regarding how you would like the unwinding to look, I can try to help you out.