11380

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

Question:

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

Answer1:

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

Recommend

  • Swift 2 Unable to remove optional binding
  • How to use angular material 6 autocomplete with data server
  • Call MailMerge.OpenDataSource() Set Connection
  • Multi Column Listbox
  • cakephp get schema for list of tables
  • How to use a detail disclosure button in UICollectionViewCell?
  • Set PageIndex of DataPager
  • When to pass by value?
  • Swift 3, iOS 10.3 - Preload UIWebView during Launch Screen
  • Indexing failed. Rolled back all changes. (Solr DataImport)
  • KendoUI Grid row filter with dropdown for boolean
  • WSO2 API Manager - Setting 'CacheId' when clustering with SQL Server
  • Grails error: table or view does not exist
  • IOS: automatic reference counting
  • Adding a Dropdown inside Kendo Grid
  • Faster Method to Making DataGridViewRow's non-Visible
  • JMeter pre-processed variable as part of report file name
  • ASP.NET dataset getdataBy Failed to enable constraints. One or more rows contain values violating no
  • C++ design pattern: multiple ways to load file
  • UltraWinGrid Auto Refresh
  • Get back anonymous type [duplicate]
  • How to access Environment Variable from AWS in my spring boot application
  • c# devexpress piechart series point color change
  • Change color of row programmatically in WatchKit
  • How to prompt user that edits have been made upon changing pages or sorting in Kendo Grid
  • C# Winforms - setting combobox selected value
  • Bind drop down list with Dictionary
  • Why does Apple use assign rather than weak to store a delegate?
  • Aligning image on right side of title on UIButton with imageEdgeInsets and titleEdgeInsets
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Is there a way to disable a specific event in kendo ui scheduler?
  • Word Open XML Mail Merge
  • How do I alternate colors in Flat List (React Native)
  • Submit form in a displaytag pagination
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • How to delete a row from a dynamic generate table using jquery?
  • using HTMLImports.whenReady not working in chrome
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection