0

In my project I have implemented a protocol which makes some URL call and return result, and my intent is to show result in UILabel. Following is my code :

protocol RestAPIResult
{
   func retriveDriverInfo()
}

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

getDriverInfo is in NextView which has Outlet of 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
       }

}

If Text is set in ViewDidLoad() it doesn't crash. but when i tried to set value in delegate function it crash saying found null.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
Komal Kamble
  • 424
  • 1
  • 8
  • 25
  • 3
    The view isn't loaded yet, thus the UILabel is nil. You should go over UIViewController lifecycle to understand more. – orxelm Jun 11 '16 at 07:49
  • @orxelm is correct. See this answer: http://stackoverflow.com/questions/5562938/looking-to-understand-the-ios-uiviewcontroller-lifecycle – Grimxn Jun 11 '16 at 08:43

1 Answers1

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

    }

*

Rajan Singh
  • 202
  • 2
  • 7