Fail calling back about protocol and delegate by Swift4 - ios

I m confuse about call back using protocol and delegate.
The problem is. I have two viewcontrollers vcA & vcB
and vcA have a tableView, vcB have a button.
vcA click the cell to vcB.
Then I want to click the button in vcB and do the following two things.
1.vcA tableView reloadData.
2.vcB popViewcontroller To vcA.
I can't understand how to solve this issue.
Have any sample to teach me?
Thanks.

This is the delegate solution , but it's better to put the self.tableView.reloadData() method inside viewDidAppear , as it's being called when you pop VcB
class VcA: UIViewController ,TableRefresh {
func reloadTable()
{
// reload here
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let des = segue.destination as! VcB
des.delegate = self
}
}
protocol TableRefresh {
func reloadTable()
}
class VcB: UIViewController {
var delegate: TableRefresh?
#IBAction func closeClicked(_ sender: UIButton) {
self.delegate?.reloadTable()
// pop here
}
}

I hope this could work for you:
class vcA : UIViewController {
override func viewWillAppear(_ animated: Bool) {
tableView.reloadData()
}
}
class vcB : UIViewController {
#IBAction fun button(bin : UIButton){
self.navigationController.popViewController(true)
}
}

It's not possible to call the methods of any VC objects if it's not in your memory.
For the first time, it vcB will not be in memory. Hence the object of the same is not in existence.
If you really want to call a function of that class and if its feasible crate an object of vcB and call the method.
or you might try a shared object of the VC and keep on using the same if it's feasible. you may post exact scenario so that people can suggest something better

Your vcA has a reference to the vcB, so you are able to write something like this in the vcA
class vcA {
var vcB: vcB?
...
vcB?.doSmth()
....
}
but there is no way for you to call vcA from the vcB since it doesn't have a reference to it. So to let vcA know that something happened in vcB or to call some function from vcB you can do several things.
Delegates, Key-Value Observing, Reactive Programming and some others.
Since you asked for the delegates solution lets stick to it.
The general idea behind delegates is as the name says to delegate someone else to do something. In your case, you want to delegate button click handling to the vcA. To do so, you will need a couple of things.
Next steps are just the implementation of the idea described above.
class VcA {
var vcB: VcB?
...
vcB?.delegate = self
...
vcB?.doSmth()
....
}
extension VcA: VcBDelegate {
func buttonIsClicked() {
// reload data
// pop vcB
}
}
protocol VcBDelegate: class {
func buttonIsClicked()
}
class VcB {
var delegate: VcBDelegate?
...
// when the button is clicked
// delegate handling to someone who is 'listening' for this event
self.delegate?.buttonIsClicked()
...
}
Notice how delegate in VcB is optional, meaning that if no one is signed as a delegate for VcB, the event will simply be ignored.

There are multiple ways to do this.
1: Put tableView.reloadData() in viewDidAppear() and then just pop vcB
2: When you are pushing vcB you create a reference to it and then in vcB you have a listener that you apply to the reference. like so:
class viewControllerB: UIViewController {
var reloadTableView: (() -> ())
#objc func buttonPressed() {
reloadTableView()
self.navigationController?.popViewController(animated: true)
}
}
And then:
let vcB = viewControllerB()
vcB.reloadTableView = {
self.tableView.reloadData()
}
present(vcB, animated: true)
3: Do as Sh_Khan

Related

How to popViewController and send back data

I have the following code to go back to the last view controller
self.navigationController?.popViewController(animated: true)
How do I send data back to the last view controller as I do this?
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)
}
}
One common way is to use delegate pattern. Pass the viewController back with some data using the delegate method and dismiss it from “parent” ViewController.
See these links for extra daya about delegates
link1
link2

Share results of asynchronous call between view controllers

I have a RootVC with container views for MapVC and ListVC.
Since I need to make an API call and share the results amongst both MapVC and ListVC, I created a RootVCDelegate.
protocol RootVCDelegate {
func fetch(results: [Object]) -> Void
}
in prepare(for UIStoryboardSegue:) I respectively set RootVCDelegate property to MapVC or ListVC depending on the identifier. I know this won't work, because delegate property can only have one value.
Delegation pattern won't work here, so how can I share results of async call between these view controllers?
you can make RootVCDelegate an array of delegates and then when you want to perform on the delegates do delegats.forEach { ..... } and when assigning a delegate just append the delegate to delegates
Example:
// Delegate Protocol
protocol RootVCDelegate {
fetch(results: [Objects]) //returning Void does nothing so no need to write it
}
// RootVC
class RootVC {
delegates: [RootVCDelegate]
prepare(for segue: UIStoryboardSegue) {
let destination = (segue.destination as? UINavigationController).rootViewController ?? segue.destination // how I like to handle things if there's a possibility of a UINavigationController housing my UIViewController
switch destination {
case _ as MapVC: delegates.append(destination)
case _ as ListVC: delegates.append(destination)
default: return
}
}
}
This may be what you're asking for?

Reload tableView after dismiss a viewController

I have a ViewController(VCA) with a TableView inside. From this ViewController it is possibile to call another ViewController (VCB). In this second VC it is possibile add an item to the plist used to populate the TableView in VCA. The problem is that when I save the new item and dismiss the VCB, I can't reload the TableView in VCA.
I have found a lot of examples:
How can you reload a ViewController after dismissing a modally presented view controller in Swift?
How to call reload table view after you call dismissViewController in swift?
How to reload tableview from another view controller in swift
Update the data of a TableViewController after add item
Update the first view after dismissing Popover
Table view is not getting updated after dismissing the popover?
after reading i tried with this code:
**IN VCB**
import UIKit
protocol AddItemDelegateProtocol {
func didAddItem()
}
class VCB: UIViewController {
var delegate : AddItemDelegateProtocol?
...
}
#IBAction func saveButton(_ sender: Any) {
....
self.delegate?.didAddItem()
dismiss(animated: true, completion: nil)
}
**In VCA**
class VCA: UIViewController, UITableViewDelegate, UITableViewDataSource, AddItemDelegateProtocol {
let addItemVC = VCB()
...
override func viewDidLoad() {
super.viewDidLoad()
addItemVC.delegate = self
...
}
func didAddItem() {
self.tableView.reloadData()
}
but this doesn't work. I don't
understand where I'm wrong. Could
you help me?
EDIT: my Solution
I solved in this way:
I've created a singleton in which I declare:
class DataManager {
static let shared = DataManager()
var firstVC = VCA()
.....
}
then, in viewDidLoad of VCA:
DataManager.shared.firstVC = self
now, in the saveButton of VCB, i can call:
#IBAction func saveButton(_ sender: Any) {
........
DataManager.shared.firstVC.tableView.reloadData()
dismiss(animated: true, completion: nil)
}
you can do this in two way :-
1)
Do One thing in VCA
VCA
override func viewWillAppear(_ animated: Bool){
tableView.reloadData()
}
If this does not work out then try this.
2)
create an instance of VCA in VCB and whenever you move from VCA to VCB pass the value of VCA to the instance of VCB and from there reload the table.
VCB
var instanceOfVCA:VCA! // Create an instance of VCA in VCB
func saveButton(){
instanceOfVCA.tableView.reloadData() // reload the table of VCA from the instance
dismiss(animated: true, completion: nil)
}
VCA
override func prepare(for segue: UIStoryboardSegue, sender: Any!) {
VCB.instanceOfVCA = self // Pass the value of VCA in instance of VCB while navigating through segue
}
Here you are calling table's reload data when the viewcontroller is not yet shown. i.e, Even before you dismissed the viewcontroler VCB and viewcontroller VCA is shown, you are calling reloadData.
Try calling reload data in VCA's viewWillAppear(animated: Bool) function.
Try this: make changes as below
let addItemVC : VCB? = nil
In ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
addItemVC = (storyboard?.instantiateViewController(withIdentifier: "ViewControllerID") as! SelectionViewController?)! // change ViewControllerID with your controller id
addItemVC.delegate = self
}
The code in your question is perfectly good. I use the same approach and it works like a charm.
IMHO the problem in your code is that you only refresh the table view with self.tableView.reloadData(), BUT may be you forget to refresh your data model - the data source for the table view. E.g. if you delete an entity from Core Data then you need to refetch your data and only after that reload the table view.
I managed to do it using delegate/protocol that is usually used to pass data between view controllers but in this instance I just called the function without passing data and inside this function i put ( tableView.reloadData() ) and it worked like a sweet :)
juts google "Passing data between view controllers and use the method as I explained above"

Initial func in 1st view from 2nd view (swift)

I have a button on 2nd viewController, after pressing that button, I would like to dismiss the 2nd viewController and go back to the 1st view controller and immediately call a function that coded inside 1st ViewController swift file.
May I know how can I do that? By segue?
There are many way to do this one of the best way is using protocol and delegate.
You can create one protocol and extend that protocol in your ViewController1. Now create the delegate of protocol in ViewController2 and pass reference of that delegate in the ViewController1's prepareForSegue method.
First create one protocol like this
protocol PassdataDelegate {
func passData()
}
Now extend this protocol in ViewController1 like this and pass the reference of delegate in prepareForSegue method
class ViewController1 : UIViewController, PassdataDelegate {
func passData() {
//Here call your function
self.callMyFunction()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "SegueIdentifier") {
let destVC = segue.destinationViewController as! ViewController2
destVC.delegate = self
}
}
}
Now create the delegate object of protocolin ViewController2 like this
class ViewController2 : UIViewController {
var delegate: PassdataDelegate?
//Now call the method of this delegate in Button action
#IBAction func buttonClick(sender: UIButton) {
self.delegate.passData()
//Now dismiss the controller
}
}
Note: - Here i am passing stringbut you can pass any type of object that you have declare in your delegate method.
You can refer unwind segue.
class ViewController1 {
#IBAction func doSomeStuffAfterReload(segue: UIStoryboardSegue) {
// do whatever you need to do here.
}
}
On storyboard, from ViewController2 Ctrl+Drag from the button to the exit outlet and select doSomeStuffAfterReload.
You can see it in action here: https://spin.atomicobject.com/2014/10/25/ios-unwind-segues/
Happy coding^^

Delegate implementation in between viewcontrollers in swift

I have 2 view controllers VCA and VCB.
Moving from VCA to VCB, with somevalue, is working fine
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("VCB") as! VCB
vc.entity = somevalue
self.navigationController?.pushViewController(vc, animated: true)
But for reverse, I want to call a method in VCA from VCB after uploading certain data from VCB. And after that refresh textfields valuesin VCA. I could have resfreshing code in viewwillappear in VCA but due to some reason i am n ot doing that but trying to implement delegate.
I have written some code as:
VCA:
class ProfileEditViewController:UIViewControoler, MobileVerifyDelegate{
override func viewDidLoad() {
super.viewDidLoad()
let mobileVC = MobileVerificationViewController()
mobileVC.delegate = self
}
//MARK: - Mobileverify Delegate method
func delegateMethod() {
print("a")
}
}
VCB:
protocol MobileVerifyDelegate{
func delegateMethod()
}
class MobileVerificationViewController: UIViewController{
var delegate: MobileVerifyDelegate! = nil
func certainFunction(){
//aftersuccessful upload
self?.delegate.delegateMethod()// code crashes
}
}
Thanks in advance
In your viewDidLoad of VCA you've created mobileVC but when you transition to VCB, you're creating a new instance of VCB named vc. mobileVC is unused the way it is. You have a few options:
make mobileVC a class property or set the delegate while creating vc.
The latter would be:
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("VCB") as! VCB
vc.entity = someValue
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)
On a sidenote, make your delegate confirming the class protocol so you can set the delegate as weak.
protocol MobileVerifyDelegate: class {
func delegateMethod()
}
class MobileVerificationViewController: UIViewController {
weak var delegate: MobileVerifyDelegate?
func certainFunction() {
// After successful upload
delegate?.delegateMethod()
}
}
Notice that when you set an implicitly unwrapped property, it is already nil. So it's redundant to set it as nil again.
var delegate: MobileVerifyDelegate! = nil // "= nil" is not needed
I don't know what your case is but the easiest solution would be to move the delegate method and delegate to VCB. If for whatever reason you VCA has to be the delegate class then you need to create an instance of it or pass it to VCB.
//Set delegate when you push to the new controller in VCA
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("VCB") as! VCB
vc.entity = somevalue
vc.delegate = self //Sets VCA as VCB's Delegate
self.navigationController?.pushViewController(vc, animated: true)
//Inside VCB
self.delegateMethod() //Now you can call any delegate protocol methods from VCA
Ya, Delegate is the way that you need to get what you aspected. there is some problem in completely implementing delegate in swift. here i provide link which fully guide you how to implemented delegate in swift.
Delegate In Swift
As you said that, App crash on calling delegate. that means, you method is not available in VCA or delegate has not reference of VCA. kindly check this two condition.
Thanks

Resources