简体   繁体   中英

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?

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM