I wanted to just go back to my previous page
from Attendance in going to Leave and Leave back to Attendance
LeaveViewController.swift
#IBAction func LeaveAttendanceSequeConnect(_ sender: Any) {
performSegue(withIdentifier: "LeaveAttendanceSegue", sender: nil)
}
Attendance.swift
#IBAction func LeaveUnwindSeque(segue: UIStoryboardSegue){
}
and then on my Leave Scene
I have ctrl drag to Exit
Pressing the Back button in Leave wont go back to Attendance
The segue should work without needing another segue to leave the unwind. Try this:
1) Keep your original LeaveAttendanceSequeConnect (which should have been bound with ctrl drag from your view controller in story board over to your corresponding .swift file).
2)Get rid of the LeaveUnwindSeque code and unbind that rewind segue properly by deleting the segue (p.s. is segue with a "g" not seque). Now click on the segue connection of the LeaveAttendanceSequeConnect in the storyboard. Make sure the original navigation controller has attributes and looks the same on the storyboard like figures A) and then make sure your second segue has attributes/storyboard like figure B).
A attribute:
B attribute:
B storyboard:
A: storyboard:
The signature of the unwind is incorrect.
#IBAction func LeaveUnwindSeque(segue: UIStoryboardSegue){
}
replace with
#IBAction func LeaveUnwindSeque(_ segue: UIStoryboardSegue){
}
Related
I'm trying to segue into a 'settings' View Controller by clicking a button, and I'm assembling it via code but don't know what I'm doing wrong.
This is my button code:
#IBAction func settingsButton(_ sender: UIButton) {
self.performSegue(withIdentifier: "SettingsViewController", sender: self)
}
The View Controller is called 'Settings View Controller' (unsurprisingly). The app crashes whenever I press on it in my Simulator.
It'll probably be a simple thing, but any ideas what I'm doing wrong?
You must set your segue identifier whatever you want (for your question = SettingsViewController) from storyboard :
Hope it helps...
I have two buttons - "Ok" and "Delete", and the "Ok" unwinds the segue to the last ViewController.
I want the "Delete" button to fire up the same unwind segue, but to do some action beforehand. (In my case, delete info from Firebase).
How can I combine both an action and unwind segue ? I tried calling PerformSegueWithIdentifier function but it didn't work.
Create a second unwind segue by control-dragging from your Delete button to the Exit icon. You can even use the existing #IBAction in your destination viewController. Give this segue an identifier (select the segue in the Document Outline view and set the identifier in the Attributes Inspector) such as "deleteSegue" and then in prepare(for:sender) check for the identifier and delete the info from Firebase.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "deleteSegue" {
// delete data from Firebase
}
}
Follow up question from the comments:
I want to perform an action BEFORE unwinding the segue - I want a
popup to ask the user if he really wants to delete the item. only
after that I want the item deleted and segue unwinded.
Instead of wiring the unwind segue from the Delete button, wire it from the viewController icon at the top of the VC to the Exit icon. Still give it the identifier and then call performSegue(withIdentifier: "deleteSegue", sender: self) when you want to perform the segue.
I just want to perform segue with a back button on top.
#IBAction func onFirst(_ sender: UIButton) {
performSegue(withIdentifier: "firstSegue", sender: self)
}
#IBAction func onSecond(_ sender: UIButton) {
performSegue(withIdentifier: "secondSegue", sender: self)
}
Let's say you have two view controllers, ViewControllerOne and ViewControllerTwo.
You could do that programmatically or using storyboards.
Since you seem to be using #IBOutlet I will assume you want to do it using storyboards.
First open up your Main.storyboard and make sure that ViewControllerOne (the controller you want to segue from) is embedded in a Navigation Controller.
If it's not, you can do that by clicking on your View Controller, then click on Editor in the top menu bar, go to Embed In and select Navigation Controller.
Then you can create a segue using the Interface Builder by doing control + click on the little yellow icon at the top of ViewControllerOne (not the Navigation Controller) & drag to ViewControllerTwo in the Interface Builder.
Now click on the segue that just got created, and type an identifier of your choice in the Attributes Inspector.
Then in your ViewControllerOne class, you can just perform the segue using the #IBOutlet as you mentioned :
#IBAction func onFirst(_ sender: UIButton) {
performSegue(withIdentifier: "your_segue_identifier", sender: self)
}
Just make sure that the segue identifiers match, and everything should be fine :)
You need redefine target and action for your current navigation item
self.navigationItem.leftBarButtonItem?.target = self
self.navigationItem.leftBarButtonItem?.action = #selector(self.onFirst(_:))
I am looking to build an app with two view controllers (VC1 and VC2) that each contain a timer. I would like for my user to be able to switch back and forth between these views and allow each timer to still run.
Currently, I have been successful in unwinding to either VC1 or VC2 (depending on which I set as the initial view controller), though I am having difficulty in successfully unwinding back and forth (i.e. from VC1 to VC2 back to VC1 and etc.)
I figured I might be able to accomplish this by initially segueing from VC1 to VC2 with a normal segue (from a button press), and though it will unwind back to VC1, I cannot get my app to unwind back to VC2 from here.
My two simple timer view controllers
Here is the code (for VC1):
#IBAction func unwindToSecondView(_ sender: UISwipeGestureRecognizer)
{
performSegue(withIdentifier: "unwindToSecond", sender: self)
}
#IBAction func backFromSecondView(segue: UIStoryboardSegue) {
}
And for VC2:
#IBAction func segueToFirstView(_ sender: UISwipeGestureRecognizer)
{
performSegue(withIdentifier: "unwindToFirst", sender: self)
}
#IBAction func backFromFirstView(segue: UIStoryboardSegue) {
}
I am really new to swift, and have found a couple interesting sources on here (How to unwind two view controllers and Swift Timer() will not update label after switching between views) but have not had much success otherwise -- would really appreciate any advice or ideas for different implementations. Thanks!!
Apple has published good example for that. I recommend always check Apple Developer Guide sites and source code. Your answer below. https://developer.apple.com/library/content/samplecode/SegueCatalog/Introduction/Intro.html
I found this tutorial useful.
working-with-unwind-segues-programmatically-in-swift
segue from A-B and from B-A you can just dismiss() and if you want to send data back from B-A you can create a delegate.
I am trying to understand how the unwind segue works. First I created 3 ViewControllers.
GlobalVC
-LoginVC
-RegisterVC
I set the initial page the GlobalVC (only authenticated user can see). I set a segue from:
GlobalVC to LoginVC called globalToLogin and LoginVC to RegisterVC called loginToRegister. Here is my storyboard:
For the registerToLogin direction, I used this work-around (in RegisterVC), but looks a bit ugly.
#IBAction func unwindToLogin(sender: AnyObject) {
//Dismiss View
dismissViewControllerAnimated(true, completion: nil)
}
However, now I am trying to direct from LoginToGlobal
First, I selected Login button, dragged it to LoginVC:
#IBAction func loginButton(segue: UIStoryboardSegue) {
performSegueWithIdentifier("unwindToGlobal", sender: self)
}
After setting segue: UIStoryboardSegue, now it gets available to right-click on Exit. But, I got confused here. Whatever I tried to drag the circle inside Exit to, it gives an error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', >reason: 'Receiver (0x7fdd2068a480>) has no segue with identifier 'unwindToGlobal''
Where should I drag the circle on? ( I keep seeing Unwind segue to 'Exit' )
The unwind action method is defined in the destination view controller (e.g. in GlobalVC).
#IBAction func unwindToGlobal(segue: UIStoryboardSegue) {
// this may be blank
}
And then, when you're hooking up the button to this action, you don't drag to the code sample, but rather to the "exit outlet" icon above the scene:
See this answer for more screen snapshots.