一次一个 UIButton

发布于 2024-09-13 00:15:30 字数 1320 浏览 5 评论 0原文

这是我一直在玩的一些代码;由于某种原因,我无法让它一次创建一个按钮。例如你有; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花ソ最美的依靠 2024-09-20 00:15:30

为什么不直接删除 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...

像你 2024-09-20 00:15:30

您过度释放每个按钮,导致其从视图中消失。当您使用 [UIButton buttonWithType...] 创建按钮时,它会被添加到自动释放池中,并且您没有它的所有权。当您将其添加到视图时,其保留计数会增加 1,从而授予您所有权,但随后您可以使用 [button release] 再次释放它。删除[按钮释放]线,一切都应该很好。

我强烈建议您阅读 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];
   }

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.

- (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];
   }
狂之美人 2024-09-20 00:15:30

嗯,这样就可以了,我觉得自己很愚蠢。昨天我差点就成功了,结果却把球丢了。因此,这里为大家回顾一下最终的完整代码:

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

[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 为实现此功能所做的所有努力。

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]:

@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);
}

Special Thanks to Run Loop for all the hard work to get this working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文