简体   繁体   中英

Delegate always getting nil value in dynamic framework class

I used delegation for passing data to ViewController B to A in dynamic framework . B is my dynamic framework ViewController . A is my app ViewController . I am always set delegate as self in my A class Without dynamic framework it works perfectly

Class B code : Inside dynamic framework (Using .xib)

import UIKit
public protocol MediaDataDelegate: class{
       func mediaDidFinish(controller: 
       LoginViewController,transactionId:String,returnURL: String)
}

public class LoginViewController: UIViewController {
public var message = ""
public var delegate: MediaDataDelegate?
public init() {
    super.init(nibName: "LoginViewController", bundle: Bundle(for: LoginViewController.self))
    print("message 1234 :\(message)")
}
required public init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
override open func viewDidLoad() {
    super.viewDidLoad()

    print("message 1234 :\(message)")
}
public class func logToConsole(_ msg: String) {
    print(msg);
}

@IBAction func backToMainBtnTapped(_ sender: UIButton) {

    self.delegate?.mediaDidFinish(controller: self, transactionId: "WERTYQWRCT", returnURL: "www.media.com")
}
}

Class A Code:Inside Other App (Using Storyboard)

Click on conduct IPVButton navigate to dynamic framework view controller I also pass some value to message string but in dynamic framework class getting empty string.

import UIKit
import NBView
class ViewController: UIViewController ,MediaDataDelegate{

var loginVC = LoginViewController()
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    LoginViewController.logToConsole("hello media")
}

@IBAction func conductIPVBtnTapped(_ sender: Any) {

    loginVC.delegate = self
    present(loginVC, animated: true, completion: nil)
}
func mediaDidFinish(controller: LoginViewController, transactionId: 
String, returnURL: String) {
    print("Trans Id\(transactionId)")
    print("return URl \(returnURL)")
}
}

It is because you are instantiating incorrectly the LoginViewController

You need to do it this way, since you wrote that you have it in a .xib file:

let loginVC = LoginViewController(nibName: yourNibName, bundle: nil)

Always have a weak reference to your delegate , otherwise you will have a retain cycle:

weak var delegate: MediaDataDelegate?

Also, you don't need to use public everywhere where you thought it might fit. Use it wisely and when needed. Here you don't need it. Remove it from everywhere in your LoginViewController

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM