【问题标题】:One UIButton at a time一次一个 UIButton
【发布时间】:2010-08-06 08:00:01
【问题描述】:

这是我一直在玩的一些代码;由于某种原因,我无法让它一次创建一个按钮。例如你有; for(i = 1; i

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

谢谢,

【问题讨论】:

  • 首先,不需要释放button,没有alloc,new或者copy。那么你看到了什么或没看到什么?

标签: iphone button uibutton iteration


【解决方案1】:

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

【讨论】:

  • 当然,我想将 for 循环运行到 MAX NUMBER,即 i
  • 您误解了 for 循环的用途。 for 循环将按照指示进行多次迭代而不停止(除非指示中断,但这将停止循环的执行,而不是暂停用户输入)。看看 Run Loop 的答案。这就是你想要的,并且不像我建议的那样使用for 循环。
【解决方案2】:

您过度释放每个按钮,导致它从视图中消失。当您使用 [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];
   }

【讨论】:

  • 嗨,感谢您的建议 - 我会查看 Apple 资源,但是,是的,代码会同时给我 4 个按钮。但我一次无法获得 1 个按钮。这就是我想要做的。应该有一种方法可以为每个 clickMe 生成 1 个按钮。
  • 为什么不把 i
  • 我一开始就试过了。没用。只按了一个按钮就停了下来。问题是这应该是一种 C 迭代。程序启动;没有按钮 - AddItem "+" 在按钮上生成。 AddItem“+”生成第二个按钮......但我没有得到这个。 [计数= 0; for(;;) if (count == 4) 中断; NSLog(@"count", count)] 或类似的东西。
【解决方案3】:

嗯,这很好,我觉得很愚蠢。我昨天差点把它丢了。所以在这里回顾一下,这里是每个人的最终完整代码:

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

[h 文件]:

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

[m 文件]:

@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 为完成这项工作所做的所有辛勤工作。

【讨论】:

  • 只是一个简短的评论;我不断添加回“tempBName.center”的原因是因为在我所做的事情中,我需要将按钮的位置移动到屏幕的中心线。如果我把它拿出来,按钮就会移动到屏幕的最左侧。我只是想解释一下。
  • 但是,经过一些实验,我发现我们可以摆脱“tempBName.center”并替换为;浮动 startPositionX = 120.0; tempBName.frame = CGRectMake(0.0+(startPositionX+buttonCount), 20.0+(startPositionY*buttonCount), 80.0, 50.0);这同样有效。再说一遍,Run Loop 是正确的。谢谢!
  • 关于此的另一个相关问题;如何停止 empBName.tag = buttonCount;从被覆盖。我试过 tempBName.tag = newButtonCount NSInteger(buttonCount);但它没有用。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-15
  • 2021-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多