Reputation: 12004
This is a beginner's question about the fundamentals of objective programming (at least I think so..):
I have an UIView and would like to add several buttons to this UIView programatically. How many buttons there are depends on the user input.
Every button has four characteristics:
Now I thought I can create a button by creating a method, but I am not entirely sure if this is a very sound method. I have the feeling that I should have created an object. Any help of how I should go about this would be very much appreciated. Here is my attempt - which is full of mistakes (for instance, I wasn't sure of how to create button1, button2 etc. -- hence the button[bNumber] thing):
-(void) createIndexButtonWithNumber:(NSString *)bNumber withTitle:(NSString *)bTitle atPosition:(NSInteger *)bPosition withColour:(NSInteger *)bColour {
CGRect buttonFrame = CGRectMake(0,bPosition,25,25);
UIButton* button[bNumber] = [[[UIButton alloc] initWithFrame:buttonFrame] autorelease];
UIButton.text = bTitle;
if (bColour == 1) {UIButton.color = [UIColor colorWithRed:0.751 green:0.742 blue:0.715 alpha:1.000];};
[indexView addSubview:button[bNumber]];
// to do: if button[bNumber] pressed {do something};
}
Then, I would like to 'multiply' the object three times by calling:
for (temp = 0; temp < 2; temp++) {
[self createIndexButtonWithNumber:temp withTitle:[@"Test%i", temp] atPosition:10 withColour:1];}
I'm sure this is all a bit problematic, wrong and clumsy, so I'd be very grateful for any suggestions of how to tackle this.
Upvotes: 0
Views: 340
Reputation: 6102
I'd like to reference to UIButton Class Reference
You should create buttons only with buttonWithType:
method.
Second, there is no button color. You can set a color for title with setTitleColor:forState:
method.
There are just syntax mistakes in your code (UIButton.text
, etc). You cant do such a call. UIButton
is a class.
Your calling createIndexButtonWithNumber...
within for
will place buttons in the same place — atPosition
input parameter has no change.
NSInteger
variable is not a pointer — *
is not needed.
...
In the end of struggling we have smth like that:
-(void)createIndexButtonWithNumber:(NSInteger)bNumber
//(sorry for that, some troubles with code formatting)
withTitle:(NSString *)bTitle
atPosition:(NSInteger)bPosition
withColour:(NSInteger)bColour {
CGRect buttonFrame = CGRectMake(0,bPosition,25,25);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = buttonFrame;
button.tag = bNumber;
[button setTitle: [NSString stringWithFormat:@"Button %i", bNumber]
forState:UIControlStateNormal];
if (bColour == 1) {
[button setTitleColor:[UIColor colorWithRed:0.751 green:0.742 blue:0.715 alpha:1.000]
forState:UIControlStateNormal];
};
// you should pass to @selector a description like 'indexAction:' but NOT any calls like '[self indexAction:button]'
[button addTarget:self
action:@selector(indexAction:)
forControlEvents:UIControlEventTouchUpInside];
[indexView addSubview:button];
}
And for
call:
for (int temp = 0; temp < 2; temp++) {
[self createIndexButtonWithNumber:temp withTitle:[@"Test%i", temp] atPosition:30 * temp withColour:1];
}
UPDATE: Selector method
-(void)indexAction:(id)sender {
NSLog(@"Button with tag %d is pressed", ((UIButton *)sender).tag);
}
Upvotes: 1
Reputation: 2809
Here's a simple example that I wrote that creates 5 buttons vertically in a row. The only thing extra in there is the tag, which is used to later reference the button if you need to.
I would suggest reading some more object orientated program examples as well.
float buttonPadding = 20;
float buttonWidth = 80;
float buttonHeight = 40;
for (int k=0;k<5;k++)
{
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.tag = 1000 + k;
btn.frame = CGRectMake(0, k*(buttonPadding+buttonHeight), buttonWidth, buttonHeight);
[btn setTitle:[NSString stringWithFormat:@"Button %i", k+1] forState:UIControlStateNormal];
[self.view addSubview:btn];
}
Upvotes: 2