Perform Segue Programmatically and Pass Parameters to the Destination View

Pass variable returned Object via Segue depending on which button pressed

I'll add my comment as an answer instead, to make it easier to show some code examples.

Add a property to your viewcontroller:

var selectedWorkout : FinalWorkout!

in each of your three button action methods you set this property to the workout associated with each button. So for your standard workout:

@IBAction func standardWorkoutPressed(_ sender: UIButton) {
let finalTimeForWorkout = Int(timeInputField.text!)

self.selectedWorkout = FinalWorkout(generatedWorkout: WorkoutGenerator.standardWorkout.generate(), timeForWorkout: finalTimeForWorkout!)
performSegue(withIdentifier: "goToWorkout", sender: self )
}

Finally:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToWorkout" {
let finalWorkoutTime = selectedWorkout.timeForWorkout
let finalWorkoutExercises = selectedWorkout.generatedWorkout.workoutExercises
if let destVC = segue.destination as? WorkoutController {
destVC.selectedWorkoutExerciseArray = finalWorkoutExercises
destVC.selectedWorkoutTime = finalWorkoutTime
}
}
}

how can i pass data using dismiss?

An other way it’s to use protocole and delegate which is very common.

I’ll recommend you this link, to have a good start

https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/#back-properties

Segue Methods and Their Objectives

You have to call

func performSegue(withIdentifier identifier: String, 
sender: Any?)

The above method will initiate your segue and you can push your next view controller.

For your info

func shouldPerformSegue(withIdentifier identifier: String, 
sender: Any?) -> Bool

The method will return Boolean value, that will specify if to perform segue or not. You have controller over it and you can allow segue to perform or not by return true/false from method.

true - Segue is allowed and will be performed

false - Segue is not allowed and will be aborted.

You can use this method if you want to override any segue you have defined in storyboard and want to perform any other at run time.

perform segue to view controller behind tab and nav-controller

Hi here i got the instance of Fourth VC

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func buttonTapped(sender: AnyObject) {
print("ViewController - buttonTapped()")
performSegue(withIdentifier: "seg4", sender: self)
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "seg1" {
let tabVC = segue.destination as! UITabBarController
let navVC = tabVC.viewControllers![0] as! UINavigationController
let destVC = navVC.viewControllers[0] as! Seq1 // ==> this transition is working

print(destVC)

}
else if segue.identifier == "seg4"{
let tabVC = segue.destination as! UITabBarController
let navVC = tabVC.viewControllers![3] as! UINavigationController
let destVC = navVC.viewControllers[0] as! Seq4 // ==> this transition is working
destVC.hello()
print(destVC)
}

}

}

class Seq1: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

class Seq2: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}
class Seq3: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.


}



override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}
class Seq4: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
print("Hello")
// Do any additional setup after loading the view, typically from a nib.
}

func hello() {
print("Hello")
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Note: The tabbar controller will always shows FirstVC. Because it is the default selection. If you want, change the selectedIndex = 3



Related Topics



Leave a reply



Submit