Search code examples
uiviewcontrollerios8.1objective-c-category

How to reference View Controller objects from it's category?


I have a iPad app for which I created a category for one of the View Controllers; how can I access some labels on a Storyboard scene for that View Controller from the category?

This is the code from the category:

    if( ([highValue compare: zero] == NSOrderedDescending) && ([lowValue compare: nines] == NSOrderedAscending) &&
   ([computedAverageValue compare:zero] == NSOrderedDescending))  {  //  if there are NO new books, don't display anything
    oHighPriceNew.text = [formatter stringFromNumber: highValue];
    oLowPriceNew.text = [formatter stringFromNumber: lowValue];
    oAveragePriceNew.text = [formatter stringFromNumber: computedAverageValue];
}

This is the error for all three labels:

Use of undeclared identifier 'oHighPriceNew'

They are declared and connected in the View Controller...

enter image description here


Solution

  • You need to refer the properties either with

    self.oHighPriceNew.text = [formatter stringFromNumber: highValue];
    self.oLowPriceNew.text = [formatter stringFromNumber: lowValue];
    self.oAveragePriceNew.text = [formatter stringFromNumber:computedAverageValue];
    

    or

    _oHighPriceNew.text = [formatter stringFromNumber: highValue];
    _oLowPriceNew.text = [formatter stringFromNumber: lowValue];
    _oAveragePriceNew.text = [formatter stringFromNumber:computedAverageValue];