King
King

Reputation: 2025

Delegate and Callback not working for passing Model data

I am trying to pass data from a firstVC to a second VC I have tried using delegate but it never worked (did not show required response) so I tried callback too and it now working so I am pasting both lines of code so any help is welcomed

Delegate:

protocol RatingDelegate: class {
    func didLoadRating(ratings : [RatingModel])
}

the viewcontroller which the data would be passed from

ViewController A:

var delegate : RatingDelegate?
func showRatings(ratings: [RatingModel]) {

        if delegate != nil {
            delegate?.didLoadRating(ratings: ratings)
        }
    }

where the delegate value is supposed to me printed

RatingVC:

extension RatingVC: RatingDelegate {
    func didLoadRating(ratings: [RatingModel]) {
        log(ratings)
    }

}

The callback Version

The view controller that would get the data

var ratingsCallBack: (() -> ([RatingModel]))?

the view controller which the value would be passed from

func showRatings(ratings: [RatingModel]) {

        let ratingVC = RatingVC()
        ratingVC.ratingsCallBack!() = {[unowned self] in
            return ratings
        }
    }

this how ever throws a response saying

Expression is not assignable: function call returns immutable value

Upvotes: 0

Views: 817

Answers (2)

Dima Paliychuk
Dima Paliychuk

Reputation: 303

It looks strange:

var ratingsCallBack: (() -> ([RatingModel]))?

should be something like this:

var ratingsCallBack: (([RatingModel]) -> ())?

so in case with callback:

class A: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let ratingVC = RatingVC()
        ratingVC.ratingsCallBack = { arr in
            arr.forEach({ (model) in
                print(model.rating)
            })
        }
        navigationController?.pushViewController(ratingVC, animated: false)
    }
}

class RatingVC: UIViewController {

    var ratingsCallBack: (([RatingModel]) -> ())?

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    @IBAction private func someButtonAction(_ sender: Any) {
        let arr = [RatingModel.init(rating: 5), RatingModel()]
        ratingsCallBack?(arr)
    }
}

struct RatingModel {
    var rating: Int = 1
}

Then when you press "someButton" you get this array in controller "A"

Upvotes: 0

jastrada
jastrada

Reputation: 394

So the FirstVC passes data to RatingVC.

On FirstVC, at the point were you invoke RatingVC you should assign the delegate.

let ratingVC = RatingVC()
self.delegate = ratingVC //Here you specify RatingVC is the delegate variable

self.present(ratingVC, animated: true)

also

if delegate != nil {

}

is unnecessary, just do delegate?.didLoadRating(ratings: ratings) to keep it cleaner


EDIT: For the callback version is the same, just assign the value to the callback before initializing the view controller that sends the data.

Upvotes: 1

Related Questions