swift protocol delegate always return nil

user979331

I have this protocol delegate defined in my View Controller:

protocol PickerDelegate : NSObjectProtocol {
    func updateMessage(meesage: String)
}

and then I called this in my View Controller:

class GradingController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, SDataGridDataSourceHelperDelegate, SLAIssuedFinalGradingDelegate, CityApprovalIssuedDelegate, CityCommentReceivedDelegate, DepositReceivedDelegate, UIPopoverPresentationControllerDelegate {

    var pickerDelegate: PickerDelegate?

}

And then I am calling my method inside the protocol delegate (this is where its nil):

func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {

    let controller = popoverPresentationController.presentedViewController as! CommentsController
    pickerDelegate?.updateMessage(meesage: controller.commentView.text)

}

And I am using this delegate in my custom class:

class TextCell: SDataGridCell, PickerDelegate {

    var dataGrid: ShinobiDataGrid?

    private var _commentText = ""

    private var label: UILabel?

    var commentText: String {

        get {
            return _commentText
        }

        set(commentText) {

            if(commentText != "")
            {
                label?.text = commentText
            }
            else
            {
                label?.text = "N/A"
            }
        }

    }

    override init(reuseIdentifier identifier: String!) {
        super.init(reuseIdentifier: identifier)

        label = UILabel()

        label?.font = UIFont.systemFont(ofSize: 15)

        label?.frame = CGRect(x: 0, y: 0, width: 200, height: 32)

        addSubview(label!)

        let pickerViewController = GradingController()
        pickerViewController.pickerDelegate = self

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    override func respondToEditEvent() {

        if dataGrid?.delegate.responds(to: #selector(SDataGridDelegate.shinobiDataGrid(_:shouldBeginEditingCellAtCoordinate:))) ?? false {
            if dataGrid?.delegate.shinobiDataGrid!(dataGrid, shouldBeginEditingCellAtCoordinate: coordinate) == false {
                return
            }
        }

        if dataGrid?.delegate.responds(to: #selector(SDataGridDelegate.shinobiDataGrid(_:willBeginEditingCellAtCoordinate:))) ?? false {
            dataGrid?.delegate.shinobiDataGrid!(dataGrid, willBeginEditingCellAtCoordinate: coordinate)
        }

    }

    func updateMessage(meesage: String) {
        commentText = meesage
    }
}

But the updateMessage method is not being called, my delegate is nil in my View Controller when I try to use it in popoverPresentationControllerDidDismissPopover but it always return nil :(

What am I doing wrong?

This is the TextCell in GradingController:

func dataGridDataSourceHelper(_ helper: SDataGridDataSourceHelper!, populateCell cell: SDataGridCell!, withValue value: Any!, forProperty propertyKey: String!, sourceObject object: Any!) -> Bool {

        let cellDataObj = object as? GradingData

        if(propertyKey == "GradingRepair")
        {

            let textCell = cell as? TextCell

            textCell?.dataGrid = self.grid

            textCell?.commentText = (cellDataObj?.GradingRepair)!

            return true
        }

    return false

    }
matt

Consider what this code does:

    let pickerViewController = GradingController() // 1
    pickerViewController.pickerDelegate = self // 2
    // 3
  1. You create a completely new GradingController.

  2. You assign the GradingController a pickerDelegate.

  3. Nothing. So you throw the GradingController away. Your code thus has no effect on anything.

What you need to do is to assign a pickerDelegate to the actual GradingController that's in your interface. But that's not what you did.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related