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
- Create
protocol
inChildViewController
- Create
delegate
variable inChildViewController
- Extend
ChildViewController
protocol inMainViewController
- Give reference to
ChildViewController
ofMainViewController
whennavigate
- Define
delegate
Method inMainViewController
- Then you can call
delegate
method fromChildViewController
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
Required Initializers for a Subclass of Uiviewcontroller
I Want to Switch Time Scrubber to Do 15 Min Intervals
Why Obj-C Instance Have 1 Retain Count Just Created
How to Get the Index (String.Index) Value from the Cursor Position of a Uitextview Element in Swift
Mapbox Navigation Pod Unable to Install with Objectivec Where I am Using Xcode 9.2
How to Set the Size of an Uiviewrepresentable
Type 'Any' Has No Subscript Members in Swift 3 Xcode 8
Storing a Variable When the App Is First Installed
Swift: Extracting/Downcasting Cftype Based Coretext Types in a Cfarray
Showing Hidden View Really Slow
Swift and Nsuserdefaults - Exc_Bad_Instruction When User Defaults Empty
Swift - Set Delegate for Singleton
If Let Doesn't Unwrap Optional Value for Mkannotation's Title Property
Swift How to Add Background Color Just Around Text in a Label
Target Is Not Found, Please Reconnect the Device, Xcode:Device Support File