一次一个UIButton

时间:2010-08-06 08:00:01

标签: iphone button uibutton iteration

这是我一直在玩的一些代码;由于某种原因,我不能让它一次创建一个按钮。比如你有;  for(i = 1; i< = 12; i ++)应该意味着每次按下外部按钮时,将创建一个新按钮,直到创建了12个按钮。然后应该有一个i = 12;在某个地方休息。但是我似乎无法让这个循环起作用。非常感谢任何帮助。

//我们将按钮放在屏幕位置的Y轴上 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);

谢谢,

3 个答案:

答案 0 :(得分:1)

为什么不删除for循环?如果你只需要一个按钮而不是四个按钮,则没有理由四次运行代码...

答案 1 :(得分:0)

您过度释放每个按钮,导致它从视图中消失。当您使用[UIButton buttonWithType ...]创建按钮时,它将添加到自动释放池中,您没有它的所有权。当您将其添加到视图时,其保留计数会增加1,从而为您提供所有权,但随后您将使用[按钮释放]再次释放它。删除[按钮释放]行,一切都应该正常。

我强烈建议您阅读Apple的内存管理编程指南。

我已经通过将按钮创建移动到每次单击添加按钮时应该调用的方法来重写代码。您还需要在标题(.h)文件中声明一个名为buttonCount的NSInteger变量。

- (void)addButton {

        if (buttonCount == 4) return;
        buttonCount += 1;

        UIButton *tempBName = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [tempBName setTitle:[NSString stringWithFormat:@"button%i", buttonCount]; forState:UIControlStateNormal];
        tempBName.tag = buttonCount;
        [tempBName addTarget:self action:@selector(clickMe:)forControlEvents:UIControlEventTouchUpInside];
        tempBName.frame = CGRectMake(0.0, 50.0+(startPositionY*buttonCount)), 80.0, 50.0);
        // Removed the line setting the center, and move the positioning to the frame above
        tempBName.titleLabel.adjustsFontSizeToFitWidth = TRUE;
        [self.view addSubview:tempBName];
   }

答案 2 :(得分:0)

嗯,这样做没事,我觉得很蠢。我昨天几乎得到了它并丢球。所以回顾一下这里是每个人的最终完整代码:

这段代码有什么作用?它允许在每次点击的基础上创建一个按钮。

[h file]:

@interface testMButton1ViewController : UIViewController {
    UIButton *addButton;
}
@property (nonatomic, retain) IBOutlet UIButton *addButton;
@property NSInteger buttonCount;
- (IBAction)addButton:(id)sender;

[m file]:

@synthesize addButton;
@synthesize buttonCount;

(IBAction)addButton:(id)sender {

    float startPositionY = 60.0;

    if (buttonCount == 6) return;
    buttonCount += 1;

    UIButton *tempBName = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [tempBName setTitle:[NSString stringWithFormat:@"button%i", buttonCount] forState:UIControlStateNormal];
    tempBName.tag = buttonCount;
    [tempBName addTarget:self action:@selector(clickMe:)forControlEvents:UIControlEventTouchUpInside];
    tempBName.frame = CGRectMake(0.0, 20.0+(startPositionY*buttonCount), 80.0, 50.0);
    tempBName.center = CGPointMake(160.0, 20.0+(startPositionY*buttonCount));
        // Removed the line setting the center, and move the positioning to the frame above
    tempBName.titleLabel.adjustsFontSizeToFitWidth = TRUE;
    [self.view addSubview:tempBName];
    NSLog(@"%d", buttonCount);
}

特别感谢Run Loop所有努力工作。