Marco
Marco

Reputation: 1061

Swift 3 Dismiss after performing segue

I have three controllers connected with segues. Controller one is MyNotif, controller 2 is AddNotif and controller three is SelectInterval.

From controller 2 I have a segue to controller 3 and then back to controller 2. This is because in controller 3 i will select from a tableview a value (string) which will be inserted in a field in controller 2.

When the segue from 3 to 2 is performed the controller is not dismissed. This means that if from controller 2 I press the back button (I do not use a navigation controller, the back button is a simple button. Here's the code:)

@IBAction func backBtnPressed(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}

I will land on controller 3 instead that on controller one.

To summarise I'm trying to achieve this:

From controller 1 I go to controller 2. From here I can go to controller 3, select the string I need, perform the segue, go back to controller 2 (and till here everything works fine). Once back on controller 2 i f I click the back button i would like to go back on controller 1 (now what happens is that i go back on controller 3).

A bit complicate explain... hopefully it is a bit clear.

enter image description here

here's my segue code: AddNotif:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "SelectRepeatInterval" {
        if let destination = segue.destination as? SelectRepeatIntervalVC {
            destination.selectedRepeatingInterval = repeatingInterval
        }
    }
}

SelectInterval:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // Get Cell Label
    let indexPath = tableView.indexPathForSelectedRow!
    let currentCell = tableView.cellForRow(at: indexPath)! as! SelectRepetaIntervalCell
    selectedInterval = currentCell.repeatIntervalLbl.text!
    performSegue(withIdentifier: "IntervalSelected", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "IntervalSelected" {
        if let destination = segue.destination as? AddNotificationVC {
            destination.repeatingInterval = selectedInterval
        }
    }
}

Upvotes: 0

Views: 987

Answers (1)

Naresh
Naresh

Reputation: 344

What you are looking for is presentingViewController https://developer.apple.com/documentation/uikit/uiviewcontroller/1621430-presentingviewcontroller

if let destination = self.presentingViewController as? AddNotificationVC {
            destination.repeatingInterval = selectedInterval
}
dismiss(animated: true, completion: nil)

Here is the complete code

tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
 // Get Cell Label
 let currentCell = tableView.cellForRow(at: indexPath)! as! SelectRepetaIntervalCell
 selectedInterval = currentCell.repeatIntervalLbl.text!
 if let destination = self.presentingViewController as? AddNotificationVC { 
   destination.<your textfield name>.text = selectedInterval 
 }
 dismiss(animated: true, completion: nil) 
}

Upvotes: 3

Related Questions