Calling method in another view controller from modal view controller using a delegate

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Calling method in another view controller from modal view controller using a delegate



I am using a modal segue to create a view controller that I want to dismiss and then call a method in the origin view controller. I have used a protocol/delegate pattern but for some reason the method is never called. The code is below: Please note that I removed a lot of non-relevant code to keep it clean here



Thanks!!



VC1:


final class WorkoutViewController: UIViewController, StartWorkoutButtonDelegate

weak var dataSource: WorkoutViewControllerDataSource!
private var workout: Workout!
private var workoutDataViewController: WorkoutDataViewController?
private var workoutFinishViewController: WorkoutFinishViewController?
private let workoutFinishViewControllerSegueIdentifier = "Workout Finish View Controller Segue"

@IBOutlet private var primaryActionButton: UIButton!
@IBOutlet private var pageControl: UIPageControl!

// MARK: - Handling View Lifecycle

override func viewDidLoad()
super .viewDidLoad()
let workoutType = dataSource.workoutType(for: self)
workout = Workout(workoutType: workoutType, managedObjectContext: persistentContainer.viewContext)
setPrimaryActionButtonLabel()


// MARK: - Handling Storyboard Segues

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
super.prepare(for: segue, sender: sender)

if let destinationWorkoutFinishViewController = segue.destination as? WorkoutFinishViewController
workoutFinishViewController = destinationWorkoutFinishViewController



// MARK: - Managing Start and Pause

@IBAction func startOrPauseWorkout()
if workout.isPaused
startWorkout()
else
pauseWorkout()






VC 2:


protocol StartWorkoutButtonDelegate: AnyObject
func startOrPauseWorkout()


class WorkoutFinishViewController: UIViewController

weak var delegate: StartWorkoutButtonDelegate?

@IBAction func startWorkout()
self.delegate?.startOrPauseWorkout()
self.dismiss(animated: true, completion: nil)


@IBAction func finishWorkout()


override func viewDidLoad()
super.viewDidLoad()






1 Answer
1



Here set the delegate


override func prepare(for segue: UIStoryboardSegue, sender: Any?)
super.prepare(for: segue, sender: sender)
if let destinationWorkoutFinishViewController = segue.destination as? WorkoutFinishViewController
destinationWorkoutFinishViewController.delegate = self






Thanks man!!!! Works perfectly!
– Starkus
Aug 12 at 14:33






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard