Objective C:从数组创建 uibuttons 时出错?
我被困在以下代码中:
btn1 = [UIButton alloc];
btn2 = [UIButton alloc];
btn3 = [UIButton alloc];
btn4 = [UIButton alloc];
btn5 = [UIButton alloc];
btn6 = [UIButton alloc];
NSArray *myarray = [NSArray arrayWithObjects:btn1, btn2, btn3, btn4, btn5, btn6,nil];
for (int i =0; i < [myarray count]; i++)
{
NSLog(@"What: %@", [myarray objectAtIndex:i]); // Result UIButton
[myarray objectAtIndex:i].frame = CGRectMake(i* 110+25, 60, 100, 100);// error, cant build
UIButton *b = (UIButton *)[myarray objectAtIndex:i];
b.frame = CGRectMake(i* 110+25, 60, 100, 100);
[b setTitle:@"my button" forState:UIControlStateNormal];
b.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
b.titleLabel.textAlignment = UITextAlignmentCenter;
b.titleLabel.textColor = [UIColor colorWithRed:0x93/255.0 green:0x73/255.0 blue:0x47/255.0 alpha:1.0];
b.tag = i+1;
[b addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
[self addSubview: b];
[b release];
}
最初我使用 switch case,例如:
switch (i)
case 0: {
/*
.....
repeat this code
.....
}
因此,如果使用 switch case,代码会很长。我循环它的原因是:
- 位置
- 尽量不要重复代码,例如设置标题,背景,添加目标等
或者您有更简单/更短的代码吗?
I am stuck at this following codes:
btn1 = [UIButton alloc];
btn2 = [UIButton alloc];
btn3 = [UIButton alloc];
btn4 = [UIButton alloc];
btn5 = [UIButton alloc];
btn6 = [UIButton alloc];
NSArray *myarray = [NSArray arrayWithObjects:btn1, btn2, btn3, btn4, btn5, btn6,nil];
for (int i =0; i < [myarray count]; i++)
{
NSLog(@"What: %@", [myarray objectAtIndex:i]); // Result UIButton
[myarray objectAtIndex:i].frame = CGRectMake(i* 110+25, 60, 100, 100);// error, cant build
UIButton *b = (UIButton *)[myarray objectAtIndex:i];
b.frame = CGRectMake(i* 110+25, 60, 100, 100);
[b setTitle:@"my button" forState:UIControlStateNormal];
b.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
b.titleLabel.textAlignment = UITextAlignmentCenter;
b.titleLabel.textColor = [UIColor colorWithRed:0x93/255.0 green:0x73/255.0 blue:0x47/255.0 alpha:1.0];
b.tag = i+1;
[b addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
[self addSubview: b];
[b release];
}
Originally I use switch case, like:
switch (i)
case 0: {
/*
.....
repeat this code
.....
}
Thus if use switch case, the codes are very long. The reason I loop it is:
- The position
- Try not to repeat the code, like set the title, background, add target etc
Or do you have a simpler/shorter code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您已分配 UIButton,但尚未初始化它们。
您还需要在循环中缓存 UIButton 指针
,然后将 '[myarray objectAtIndex:i]' 的所有实例替换为 'currentButton'
You have alloced the UIButtons but have not initialized them.
You also need to cache the UIButton pointer in the loop
then replace all instances of '[myarray objectAtIndex:i]' with 'currentButton'
您需要在分配后初始化,否则您刚刚分配了垃圾内存
也会
失败,因为它不知道该对象是什么类,因此它是否可以在其上调用 setFrame: ,这就是为什么您需要将其转换为按钮(这是UIView 子类)
You need to init after alloc otherwise you have just allocated garbage memory
Also
fails because it does not know what class the object is and therefore if it can call setFrame: on it, which is why you need to cast it as a button (which is a UIView subclass)
你可以试试这个吗?
我认为您忘记初始化按钮。有不同的方法来初始化它们。 init、initWithFrame 和 initWithCoder
You may try this?
I think you forgot to initialize your buttons. There are different ways to initialize them. init, initWithFrame and initWithCoder
给您一个错误,因为您无法使用点语法来访问强制转换(或在本例中为
id
)对象。如果删除此行并在设置b
之后添加以下内容:您将不再收到错误。
您还可以使用快速枚举来优化循环:
其他回答者正确地指出您一开始就没有正确创建按钮 - 使用
buttonWithType
或alloc
和initWithFrame
。Is giving you an error because you can't use dot syntax to access cast (or in this case,
id
) objects. If you remove this line and put the following after you have setb
:You will no longer get the error.
You could also optimise your loop using fast enumeration:
The other answerers are correct in pointing out that you aren't creating the buttons properly in the first place - use
buttonWithType
oralloc
andinitWithFrame
.