简体   繁体   中英

Objective-c: Multiplying an object

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:

  1. Its number (eg button number one, two, three etc.)
  2. Its title (eg click this, click here, click me etc.)
  3. Its position and size (ie a CGRect - size stays the same, only y-position needs to change)
  4. Its colour (eg red, green, blue etc.)

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.

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];
}

I'd like to reference to UIButton Class Reference

  1. You should create buttons only with buttonWithType: method.

  2. Second, there is no button color. You can set a color for title with setTitleColor:forState: method.

  3. There are just syntax mistakes in your code ( UIButton.text , etc). You cant do such a call. UIButton is a class.

  4. Your calling createIndexButtonWithNumber... within for will place buttons in the same place — atPosition input parameter has no change.

  5. NSInteger variable is not a pointer — * is not needed.

  6. ...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM