我有一个带有许多.xib文件(UIViews)和许多UIButton的ViewController。 所以我们的想法是将它们保存在一个数组[ViewID] [ButtonID]中,以便使用计数器,数字等方便地访问它们。
***ViewController.h
IBOutlet UIButton *button0101;
IBOutlet UIButton *button0102;
IBOutlet UIButton *button0103;
IBOutlet UIButton *button0201;
...
NSMutableArray *buttonsArray;
...
@property (nonatomic, retain) IBOutlet UIButton *button0101;
@property (nonatomic, retain) IBOutlet UIButton *button0102;
@property (nonatomic, retain) IBOutlet UIButton *button0103;
@property (nonatomic, retain) IBOutlet UIButton *button0201;
...
@property (nonatomic, retain) NSMutableArray *buttonsArray;
***ViewController.m
@synthesize allThoseButtons/Arrays;
...
-(void)viewDidLoad {
self.buttonsArray = [[NSMutableArray alloc] init];
NSMutableArray *helpArray = [[NSMutableArray alloc] init];
[helpArray addObject:self.button0101];
[helpArray addObject:self.button0102];
[helpArray addObject:self.button0103];
[self.buttonsArray addObject:helpArray];
[helpArray removeAllObjects];
[helpArray addObject:self.button0201];
...
[self.buttonsArray addObject:helpArray];
[helpArray removeAllObjects];
...
我可以访问数组,我可以输出正确的预定义按钮标题等,但我无法在使用buttonsArray访问它们时更改按钮(即backgroundimage,titleLabel等)。
-(IBAction)pushButton:(id)sender {
//this does work
[(UIButton *)sender setTitle:@"Test" forState:UIControlStateNormal];
//this has no effect at all (even if I have chosen the correct button in the array, I can check that using the predefined tag and title)
[(UIButton *)[helpButtons objectAtIndex:index2] setTitle:@"Test" forState:UIControlStateNormal];
}
似乎保存在NSMutableArray中的引用与InterfaceBuilder中的引用不同,而是一个新对象。如果我直接用NSLog输出对象,我甚至可以看到另一个内存ID。 但是我做错了什么? 如何在数组中保存实际引用以访问这些按钮?
谢谢和问候, 专利