swift协议委托总是返回nil

时间:2018-04-22 15:11:51

标签: ios swift

我在View Controller中定义了此协议委托:

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

    var pickerDelegate: PickerDelegate?

}

然后我在我的View Controller中调用了它:

func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {

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

}

然后我在协议委托中调用我的方法(这是它的nil):

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
    }
}

我在自定义类中使用此委托:

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

    }

但是没有调用updateMessage方法,当我尝试在popoverPresentationControllerDidDismissPopover中使用它时,我的委托在我的View Controller中为nil但是它总是返回nil :(

我做错了什么?

这是GradingController中的TextCell:

exrate = 100
sales = 0

def sim(var, min, max):
    for var in range(min, max):
        turnover = 1000 * (exrate/100) + sales
        print(turnover)

1 个答案:

答案 0 :(得分:1)

考虑这段代码的作用:

    let pickerViewController = GradingController() // 1
    pickerViewController.pickerDelegate = self // 2
    // 3
  1. 您创建了一个全新的GradingController。

  2. 您为GradingController指定了pickerDelegate

  3. 无。所以你扔掉了GradingController。因此,您的代码对任何内容都没有影响。

  4. 您需要做的是将pickerDelegate分配给您界面中的实际GradingController。但那不是你做的。