Use existing unwind segue from another action - ios

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.

Related

go back to a certain viewController

my Storyboard looks like:
User can go directly from the 1st screen to the 4th. The fourth screen contains tableView, with XIB cell design. I want user to be able to tap on a cell and get to the 3rd screen and send some data with that. I know this should be done in didSelectRowAt. But is it even possible?
Yes it is possible.
Create a segue. In your storyboard, ctrl-drag from the first button on top of your 4th screen to anywhere on your second screen. You should see a new segue created in your storyboard.
Give the segue an id. Click on the newly created segue, in the right panel, set the indentifier attribute under the Indentity Inspector tab.
Perform the segue. In your didSelectRowAt, add the following line:
self.performSegue(withIdentifier: "THE_ID_YOU_ASSIGNED_IN_STEP_2")
Send data to the destination segue. In your screen 4 view controller, add the following function:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "THE_ID_YOU_ASSIGNED_IN_STEP_2" {
if let destinationVC = segue.destination as? YOUR_SCREEN_3_VC {
// Send data to your VC, such as assigning values
}
}
}

Go back to previous page using unwind segue

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){
}

How to add If statement before unwinding segue? [duplicate]

This question already has answers here:
how to perform Condition check, before segue unwind
(2 answers)
Closed 5 years ago.
I have 2 views: FirstView and SecondView.
On SecondView, there are a textfield and a Submit button.
If user click Submit button, check if textfield is empty or not before going back to FirstView.
On StoryBoard of SecondView, I create a Unwind Segue by hold Ctrl and click on Submit button, then drag to Exit and choose tapToBackToFirstView
On FirstViewController:
#IBAction func tapToBackToFirstView (segue: UIStoryboardSegue) {
}
On SecondViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "backToFirstViewFromSecondView" {
let destinationController = segue.destination as! FirstViewController
destinationController.resultLabel.text = secondViewTextField.Text
}
}
From my code, If I click on Submit button, it always go to FirstView.
How can I add a IF statement to check if the textfield is empty or not? If it is empty, show an alert and DO NOT go to FirstView.
If it is not empty, go to FirstView.
Thanks
On your FirstView Controller you have unwindseguemethod tapToBackToFirstView which is connected from button action right?. Now remove that and instead of connecting unwind segue through IBAction, just drag from SecondViewController to it's own exit so that you will get one manual segue created. Give a segue identifier to this.
Now connection IBAction normally just like any button. call performSegueWithIdentifier in this based on condition.
#IBAction func tapToBackToFirstView(sender: AnyObject) {
if //Add your condition here
{performSegueWithIdentifier("segue identifier", sender: self)}
else {
//Do this
}
}

check the segue used to open viewController in the viewController itself

Having this methods:
//Metodo che verifica se proveniamo dal Profile controller o da un altro controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "accountSegue" {
self.DisattivaTutorialBtn.isHidden=true
IndietroBtn.isHidden=false
}else{
self.IndietroBtn.isHidden=true
self.DisattivaTutorialBtn.isHidden=false
}
}
I perform different actions basing on the segue's identifier.
Of course, this method isn't called anywhere (I understood that it is): how can I call it? It seems stupid but calling it as "self" method in "viewDidLoad" doesn't work.
To do that instead of performing your Segue on a button click in storyboard, create a segue from your viewcontroller to your other view controller. then when you want to go to the next view controller call below from your button click or any other action event.
self.performSegue(withIdentifier: "accountSegue", sender: self)
This will automatically call your prepareForSegue method and your if statement will execute.
If you are using manual segues in your Storyboard, you can perform the segue by calling self.performSegue(withIdentifier:sender:). See performSegue documentation.
If you are using segues whose type is not set to manual, when you click on the UI element to which the segue is linked in your Storyboard, prepare(for segue) will be called automatically by the system.
To perform Segue manually you need to attach the action
Following will help you go get the required thing done
performSegue(withIdentifier: "accountSegue", sender: nil)

How to Segue to a specific view controller depending on the Segue identity, from a Collection view cell

Currently developing a small ios app in swift, I have populated a collection view cell with data from a .plist, each cell has a title and button, what i'm wanting is to segue to multiple view controllers in the storyboard depending on the segue identity once the button is pressed. For example if the segue has the id set as food, i want it to navigate to the view called food?
or if it is easier for the segue to pull the title from the cell then navigate to the view with the same title as the cell?
ill try and explain in code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "food"
{
//then navigate to the view controller called food
}
else if segue.identifier == "drink"{
{
//navigate to the view called drink
}
}
If food, drink or any other items have their own unique view in storyboard this is achievable.
You can assign a string to each button in UICollectionView and check which item in the collection was tapped and do a performSegueWithIdentifier: with button string.
performSegueWithIdentifier("toComments", sender: self)
Then prepareForSegue: method to pass data. Do another if for drink.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toFood" {
let ExchangeViewData = segue.destinationViewController as! FoodViewController
ExchangeViewData.foodMenuToShow = foodMenuID //This can be anything that you get your food items.
}
}
You can't do what you are asking for. A segue identifier is what determines which view controller gets invoked by the segue. If you want to go to a different view controller, invoke a segue with a different identifier. By the time you get to prepareForSegue it's too late. The segue to a specific view controller is already in progress.
You should explain what it is you're trying to do and we can help you solve your problem rather than trying to fight the APIs and do something that's neither possible nor appropriate. (Your question is an example of an "XY problem".)

Resources