如何在xcode中创建按钮数组?

时间:2014-03-12 12:25:09

标签: ios objective-c

我有一个包含80 UIButton s和80 UIImage s的视图。我不想通过单独的出口引用来引用它们,而是将它们称为数组中的索引,因此能够更改图像,并确定UIButton正在发送没有特定引用的消息。< / p>

我确信这一定是可能的,因为没有办法让80个不同版本的相同代码是正确的方法!

这可能吗?

2 个答案:

答案 0 :(得分:3)

通过调查UICollectionView可能会更好,但要回答问题:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.buttonArray = [NSMutableArray array];
    for (int i = 0; i < 80; i = i + 1) {
        // However you wish to get your button
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, i * 20, 20, 10);
        [self.view addSubview:button];
        // Other button-specific stuff (like setting the image, etc.)
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArray addObject:button];
    }
}
- (void)buttonPressed:(UIButton *)sender {
    int index = [self.buttonArray indexOfObject:sender];
    // Now handle the button press based
}

答案 1 :(得分:1)

有可能,它被称为出口收集。

@property(nonatomic,retain) IBOutletCollection NSArray *buttonsArray;
相关问题