一次一个 UIButton
这是我一直在玩的一些代码;由于某种原因,我无法让它一次创建一个按钮。例如你有; for(i = 1; i <=12; i++) 应该意味着每次按下外部按钮时都会创建一个新按钮,直到创建了 12 个按钮。然后应该有 ai = 12;break 某处。但是我似乎无法让这个循环工作。任何帮助将不胜感激。
// 我们将按钮放置在屏幕 Y 轴上的位置 浮动起始位置Y = 70.0;
for(int i = 1; i <= 4; i++) {
NSString *button = [NSString stringWithFormat:@"button%i", i];
// NSMutableString version to keep button from changing name.
//NSString *button = [NSMutableString stringWithFormat:@"button%i", i];
UIButton *tempBName = (UIButton *)[UIButton buttonWithType:UIButtonTypeRoundedRect];
[tempBName setTitle:button forState:UIControlStateNormal];
tempBName.tag = i;
[tempBName addTarget:self action:@selector(clickMe:)forControlEvents:UIControlEventTouchUpInside];
tempBName.frame = CGRectMake(0.0, 0.0, 80.0, 50.0);
tempBName.center = CGPointMake(160.0, 50.0+startPositionY);
tempBName.titleLabel.adjustsFontSizeToFitWidth = TRUE;
[self.view addSubview:tempBName];
// Make space between each button
startPositionY += 70;
// How many buttons out of "i" are we creating?
NSLog(@"%d", i);
// release button
[button release];
}
// Was the button Pressed?
NSLog(@"Did Press");
// Did our Position change on the Y-axis?
NSLog(@"%f", startPositionY);
谢谢,
Here is some code I have been playing with; for some reason I cannot get it to create a single button at a time. For example you have ;
for(i = 1; i <=12; i++) should mean that for each time an external button is pressed a new one is created until 12 buttons have been created. Then there should be a i = 12;break somewhere. However I cannot seem to get this loop to work. Any assistance would be greatly appreciated.
// Where we place the button on the Y-axis on the screen position
float startPositionY = 70.0;
for(int i = 1; i <= 4; i++) {
NSString *button = [NSString stringWithFormat:@"button%i", i];
// NSMutableString version to keep button from changing name.
//NSString *button = [NSMutableString stringWithFormat:@"button%i", i];
UIButton *tempBName = (UIButton *)[UIButton buttonWithType:UIButtonTypeRoundedRect];
[tempBName setTitle:button forState:UIControlStateNormal];
tempBName.tag = i;
[tempBName addTarget:self action:@selector(clickMe:)forControlEvents:UIControlEventTouchUpInside];
tempBName.frame = CGRectMake(0.0, 0.0, 80.0, 50.0);
tempBName.center = CGPointMake(160.0, 50.0+startPositionY);
tempBName.titleLabel.adjustsFontSizeToFitWidth = TRUE;
[self.view addSubview:tempBName];
// Make space between each button
startPositionY += 70;
// How many buttons out of "i" are we creating?
NSLog(@"%d", i);
// release button
[button release];
}
// Was the button Pressed?
NSLog(@"Did Press");
// Did our Position change on the Y-axis?
NSLog(@"%f", startPositionY);
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不直接删除
for
循环呢?如果您只想要一个按钮,而不是四个,则没有理由运行代码四次......Why don't you just remove the
for
loop? If you only want one button, and not four, there's no reason to run the code four times...您过度释放每个按钮,导致其从视图中消失。当您使用 [UIButton buttonWithType...] 创建按钮时,它会被添加到自动释放池中,并且您没有它的所有权。当您将其添加到视图时,其保留计数会增加 1,从而授予您所有权,但随后您可以使用 [button release] 再次释放它。删除[按钮释放]线,一切都应该很好。
我强烈建议您阅读 Apple 的内存管理编程指南。
我通过将按钮创建移动到每次单击添加按钮时都应该调用的方法来重写您的代码。您还需要在头文件 (.h) 中声明一个名为 ButtonCount 的 NSInteger 变量。
You are over-releasing each button, causing it to disappear from view. When you create the button with [UIButton buttonWithType...] it is added to the autorelease pool and you do not have ownership of it. When you add it to the view, its retain count is incremented by 1 giving you ownership, but then you release it again with [button release]. Remove the [button release] line and all should be well.
I strongly recommend you read Apple's Memory Management Programming Guide.
I have rewritten your code by moving button creation into a method which you should call each time the add button is clicked. You will also need to declare an NSInteger variable called buttonCount in your header (.h) file.
嗯,这样就可以了,我觉得自己很愚蠢。昨天我差点就成功了,结果却把球丢了。因此,这里为大家回顾一下最终的完整代码:
这段代码的作用是什么?它允许在每次点击的基础上创建一个按钮。
[h file]:
[m file]:
特别感谢 Run Loop 为实现此功能所做的所有努力。
Well, that did it all right, I feel so stupid. I nearly had it yesterday and dropped the ball. So Just to recap here is the final complete code for everyone out there:
What does this code do? It allows the creation of a single button on a per click basis.
[h file]:
[m file]:
Special Thanks to Run Loop for all the hard work to get this working.