Swift/How to Call Delegate with Popviewcontroller

Swift / how to call delegate with popViewController

You can set the delegate of My3rdVC in the prepareForSegue method of My2ndVC.

class My2ndVC: UIViewController, PassClubDelegate {

...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

super.prepareForSegue(segue, sender: sender)

switch segue.destinationController {
case let controller as My3rdVC:
controller.passClubDelegate = self
}
}
}

This is assuming you have created a segue in your storyboard that pushes My3rdVC from My2ndVC onto the navigation controller stack, which I'm assuming you have. So just try simply pasting this prepareForSegue method into My2ndVC and see if it works.

UPDATE

let vc = stb.instantiateViewControllerWithIdentifier("My3rdVC") as! My3rdVC

vc.passClubDelegate = self

navigationController?.pushViewController(vc, animated: true)

How to popViewController and send back data

Swift relies a lot on the delegate pattern and this is a good place to use it.

class FirstViewController: UIViewController {
func pushToSecondViewController() {
let second = SecondViewController()
second.firstViewControllerDelegate = self // set value of delegate
navigationController?.pushViewController(second, animated: true)
}

func someDelegateMethod() {
print("great success")
}
}

class SecondViewController: UIViewController {
weak var firstViewControllerDelegate: FirstViewController? // establish a delegate

func goBackToFirstViewController() {
firstViewControllerDelegate?.someDelegateMethod() // call delegate before popping
navigationController?.popViewController(animated: true)
}
}

Can I invoke the delegate method after popViewcontroller?

Usually, a view controller is an independent unit of screens. Especially if it's switched by navigation-controller. You are expected to reconfigure views to bind their data in one of overriding of viewWillAppear: or viewDidAppear: method.

Usually viewDidAppear: is preferred. Because in many cases, switching view needs reloading of underlying data, and this usually causes asynchronous I/O. In this case, this asynchronous I/O may interfere simultaneously performing view-switching animation.

Anyway, if your view setup operation is lightweight, it's fine and better to go with viewWillAppear: because it will make your user to wait less.

In this case, IMO, it seems your best bet is just marking to refresh the data on the target view controller, and handle refreshing in the view-controller's viewDidAppear: method.

How to send data back by popViewControllerAnimated for Swift?

You can pass data back using delegate

  1. Create protocol in ChildViewController
  2. Create delegate variable in ChildViewController
  3. Extend ChildViewController protocol in MainViewController
  4. Give reference to ChildViewController of MainViewController when navigate
  5. Define delegate Method in MainViewController
  6. Then you can call delegate method from ChildViewController

Example

In ChildViewController: Write code below...

protocol ChildViewControllerDelegate
{
func childViewControllerResponse(parameter)
}

class ChildViewController:UIViewController
{
var delegate: ChildViewControllerDelegate?
....
}

In MainViewController

// extend `delegate`
class MainViewController:UIViewController,ChildViewControllerDelegate
{
// Define Delegate Method
func childViewControllerResponse(parameter)
{
.... // self.parameter = parameter
}
}

There are two options:

A) with Segue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
let goNext = segue.destinationViewController as ChildViewController
goNext.delegate = self
}

B) without Segue

let goNext = storyboard?.instantiateViewControllerWithIdentifier("childView") as ChildViewController
goNext.delegate = self
self.navigationController?.pushViewController(goNext, animated: true)

Method Call

self.delegate?.childViewControllerResponse(parameter)

Delegates in swift?

It is not that different from obj-c.
First, you have to specify the protocol in your class declaration, like following:

class MyClass: NSUserNotificationCenterDelegate

The implementation will look like following:

// NSUserNotificationCenterDelegate implementation
func userNotificationCenter(center: NSUserNotificationCenter, didDeliverNotification notification: NSUserNotification) {
//implementation
}

func userNotificationCenter(center: NSUserNotificationCenter, didActivateNotification notification: NSUserNotification) {
//implementation
}

func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool {
//implementation
return true
}

Of course, you have to set the delegate. For example:

NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self;

how to set delegate while passing from one view controller to previous in swift 3?

At the time of redirection in FilterSelectionViewController you have to set delegate = self like this

let main = UIStoryboard(name: "Main", bundle: nil)
let vc = main.instantiateViewController(withIdentifier: "tempVC") as! tempVC// Your FilterSelectionViewController
vc.delegateColor = self //your delegate
navigationController?.pushViewController(vc, animated: true)

Protocol Delegate Method is not called in Swift

So basically in line var seconviewcontroller : SecondViewController = SecondViewController() is different from your pushing view controller instance.

You are making a separate instance of SecondViewController so you have done delegate self at the time of pushing with pushes object like that

let secondVCInstance = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
secondVCInstance.delegate = self
self.navigationController?.pushViewController(secondVCInstance, animated: true)

NOTE: - EVERY OBJECT HAS ITS OWN PROPERTIES



Related Topics



Leave a reply



Submit