gpt4 book ai didi

ios - 在数组中创建 UIButtons 并将它们添加到 MainViewController

转载 作者:行者123 更新时间:2023-11-28 20:27:07 25 4
gpt4 key购买 nike

我一直在开发一个应用程序,我在 IB 中手动创建了十几个按钮。我现在想重新开始,用代码做所有事情,因为我需要按钮的数量是动态的。我还想做其他事情,比如手动控制它们出现的位置等等。

MainViewController.h 中:

@property (nonatomic, retain) IBOutlet UIButton *startButton;
@property (nonatomic, retain) IBOutlet UIButton *stopButton;
@property (nonatomic, retain) IBOutletCollection(UIButton) NSMutableArray *tiles;
- (IBAction) tilePressed:(id)sender;
- (void) initTiles;

MainViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
[self initTiles];
}

- (void)viewDidUnload
{
[self setStartButton:nil];
[self setStopButton:nil];
[self setTiles:nil];
[super viewDidUnload];
}

-(void)initTiles
{
//tiles = [[NSMutableArray alloc] initWithCapacity:5];

for (int count = 0; count < 5; count++)
{ //I used tiles.count here, but that fails
UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(count*10+10, 10, 10, 10)];
[b setTitle:[NSString stringWithFormat:@"%i",count+1] forState:UIControlStateNormal];
[b setTag:count];
[b setHidden:NO];
[b addTarget:self action:@selector(tilePressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:b];
}
}

-(IBAction)tilePressed:(id)sender {
NSLog(@"button pressed: %i", [sender tag]);
}

我一直在尝试做的是简单地在屏幕上添加 5 个按钮。理想情况下,我想创建按钮,将它们放在一个数组 (NSMutableArray) 中,以便我稍后可以操作它们,然后将 tiles 扔到 MainViewController。但是,如果没有 tiles 对象,我什至无法将按钮连接到 Controller 。

编辑:我想我会补充一点,它们属于一个数组并不重要,重要的是我可以稍后与它们交互,即,标题会有动画和文本变化等。最初然而,问题是它们没有显示在 Controller 上

最佳答案

您的代码实际上可以完美运行,但您无法真正判断,因为:

  • 按钮太小了,几乎无法使用
  • 按钮太小以至于文本标题不会显示
  • 背景颜色为 nil,因此默认为清除,这使得整个按钮在这种情况下不可见。

为了使其更明显,只需将按钮变大并更改背景颜色即可:

-(void)initTiles
{
//tiles = [[NSMutableArray alloc] initWithCapacity:5];

for (int count = 0; count < 5; count++)
{ //I used tiles.count here, but that fails
UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(count*50+10, 50, 50, 50)];
[b setTitle:[NSString stringWithFormat:@"%i",count+1] forState:UIControlStateNormal];
[b setTag:count];
[b setHidden:NO];
[b addTarget:self action:@selector(tilePressed:) forControlEvents:UIControlEventTouchUpInside];
[b setBackgroundColor:[UIColor purpleColor]];
[self.view addSubview:b];
}
}

现在,就将它们添加到数组而言:

-(void)initTiles
{
self.tiles = [[NSMutableArray alloc] initWithCapacity:5];

for (int count = 0; count < 5; count++)
{ //I used tiles.count here, but that fails
UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(count*50+10, 50, 50, 50)];
[b setTitle:[NSString stringWithFormat:@"%i",count+1] forState:UIControlStateNormal];
[b setTag:count];
[b setHidden:NO];
[b addTarget:self action:@selector(tilePressed:) forControlEvents:UIControlEventTouchUpInside];
[b setBackgroundColor:[UIColor purpleColor]];
[self.tiles addObject:b];
[self.view addSubview:b];
}
}

然后您可以使用数组访问按钮!

关于ios - 在数组中创建 UIButtons 并将它们添加到 MainViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13596339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com