在我的项目中,我实现了一个协议,该协议可以进行一些URL调用并返回结果,而我的意图是在UILabel中显示结果。以下是我的代码:

protocol RestAPIResult
{
   func retriveDriverInfo()
}

class RestAPICall :  RestAPIResult
{
   func retriveDriverInfo()
    {
     self.dashBoardViewController.getDriverInfo(driverProfile)
    // calling another function of Next View for Lable Setup
    }
}


getDriverInfo在NextView中,它具有textVIew的出口

class DashBoardViewController: UIViewController
{
       override func viewDidLoad()
        {
        super.viewDidLoad()

        restDeledate = RestAPICall()

        restDeledate!.retriveDriverInfo()
     // IF LABEL SET HERE NO ERROR
      //totalTripLabel.text = "Testing"  // NO ERROR

        }

       func getDriverInfo(driverInfoArray : NSArray)
       {
        totalTripLabel.text = "Testing" // HERE IS ERROR
       }

}


如果在ViewDidLoad()中设置了Text,它不会崩溃。但是,当我尝试在委托函数中设置值时,它崩溃,提示找到null。

最佳答案

protocol RestAPIResult: NSObjectProtocol
    {
       func retriveDriverInfo(message: String)
    }

    class RestAPICall :  RestAPIResult
    {

    override func viewDidLoad()
            {
            super.viewDidLoad()
             //create an instance of your vc and
            instance.delegate = self
            //if you code for push vc then write this line there  insatance.delegate = self
            }
       func retriveDriverInfo(message: String)
        {
         yourLbaelOutlet.text = message
        }
    }


    class DashBoardViewController: UIViewController
    {
           weak var delegate: RestAPIResult!

           override func viewDidLoad()
            {
            super.viewDidLoad()
            }

           func getDriverInfo(driverInfoArray : NSArray)
           {
            self.delegate.retriveDriverInfo(message: "youMessage")
           }

    }


*

关于ios - 在UILabel上解开Optional值时意外发现nil,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37761242/

10-11 23:04