在UILabel上展开可选值时意外地发现了nil

时间:2016-06-11 07:41:56

标签: ios swift uilabel swift-protocols

在我的项目中,我已经实现了一个协议,它可以进行一些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。

1 个答案:

答案 0 :(得分:0)

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

    }

*