Andrew
Andrew

Reputation: 16051

How do i refer to a UIButton inside an NSMutableArray?

This is the code i've tried modifying, to no avail every time:

(UIButton *)[colourButtonsArray objectAtIndex:2].text = @"Test";

I get an error of 'Request for member 'text' in something not a structure or union.

Any ideas how to solve this?

Upvotes: 2

Views: 428

Answers (2)

theChrisKent
theChrisKent

Reputation: 15099

Call the correct method:

[(UIButton *)[colourButtonsArray objectAtIndex:2] setTitle:@"Test" forState:UIControlStateNormal];

Documentation Here: http://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/setTitle:forState:

If you just want to cast and access properties you can just add more parenthesis (although text is not an actual property):

((UIButton *)[colourButtonsArray objectAtIndex:2]).buttonType;

Upvotes: 5

GendoIkari
GendoIkari

Reputation: 11914

To avoid extra parentheses with casting, you can use method syntax instead of dot notation. However, in the case of a UIButton... you set a UIButton's text with setTitle:forState: method.

Upvotes: 3

Related Questions