views:

84

answers:

3

Hello Guys,

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,

A: 

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];
   }
Run Loop
Hi, Thanks for your advise - I will check out the Apple resource, However, yes the code will give me 4 buttons at the same time. But I cannot get 1 button at a time. That is what I am trying to do. There should be a way to generate 1 button per clickMe.
nepfable
why dont you just change the i <= 4 to i <= 1?
Run Loop
I tried that at first. Didn't work. Just made one button and stopped. Thing is this should be a type of C iteration. Program start up; no button - AddItem "+" generate on button. AddItem "+" generate 2nd button ... but I am not getting this. [count = 0; for(;;) if (count == 4) break; NSLog(@"count", count)] or something like that.
nepfable
I have updated the code above. This should do what you want.
Run Loop
+1  A: 

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

Jasarien
well of course I want to run the for loop to MAX NUMBER i.e., i <=4. To generate each of 4 buttons one at a time. I used to have break; and "==" , but they didn't seem to have any effect. I did try to use ARRAY's, but that didn't work either.
nepfable
You're misunderstanding what a `for` loop is used for. A `for` loop will iterate as many times as instructed without stopping (unless instructed to with a break, but that will halt execution of the loop, not pause for user input). Look at Run Loop's answer. That is what you want, and doesn't use the `for` loop, like I suggested.
Jasarien
Thank you and you are right. I stand corrected my friend.
nepfable
A: 

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.

nepfable
Just a quick comment; the reason I keep adding back in the "tempBName.center" is because in what I am doing I need to move the position of the button to the center line of the screen. If I take that out the buttons move to the far left side of the screen.Just I wanted to explain about that.
nepfable
However, after some experimenting I found that we can get rid of the "tempBName.center" and replace it with;float startPositionX = 120.0;tempBName.frame = CGRectMake(0.0+(startPositionX+buttonCount), 20.0+(startPositionY*buttonCount), 80.0, 50.0);This works just as well. So again, Run Loop is correct. Thanks!
nepfable
Thanks, please mark my answer as accepted.
Run Loop
Another related question on this; how to you stop empBName.tag = buttonCount; from being over written. I tried tempBName.tag = newButtonCount NSInteger(buttonCount); but it didn't work. Any ideas?
nepfable