One UIButton at a time

2019-09-21 16:00发布

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,

3条回答
Evening l夕情丶
2楼-- · 2019-09-21 16:13

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...

查看更多
可以哭但决不认输i
3楼-- · 2019-09-21 16:14

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];
   }
查看更多
一纸荒年 Trace。
4楼-- · 2019-09-21 16:29

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.

查看更多
登录 后发表回答