Create a for loop to add 39 buttons to an array

前端 未结 5 724
情书的邮戳
情书的邮戳 2021-01-02 11:07

I have 39 different UIButton variables in my .h file, but I would like to add each of them to an array without having to type out the same thing 39 times.

Is there a

相关标签:
5条回答
  • 2021-01-02 11:19

    You might want to forego the 39 buttons in your header file and instead have a single array. I suspect that you want to use manual references so you can take advantage of Interface Builder, to control events and layout. I suggest doing something a little different.

    Create a single property - an NSMutableArray. Then, when the view loads, create the buttons on the fly.

    To access a button, use something like [self.arrayOfButtons objectAtIndex:38];. (In the case of 39 buttons, that would return the last button.);`

    To create a button, you use the following:

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    

    Note that you pass in the frame of your button's init method. The frame of your button is going to start in the top left corner of its container and your button will be 100 pixels square. The frame is an instance of CGRect. (You create a CGRect by calling the function CGRectMake(x,y,width,height).

    To make 39 buttons, you might want to loop as follows, given an array to hold the buttons, myButtons and predefined numberOfButtons and dimension variables:

    for(int i=0;i<numberOfButtons;i++){
      //Create the button
      UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,y,width, height)];
      //Store the button in our array
      [self.myArray addObject:button];
      //Release the button (our array retains it for us)
      [button release];
    }
    

    Of course, you are going need to set unique values for x,y,width and height for each button or they will all overlap. Once you've created your buttons, you can do things with them, like set the label, or show them onscreen. To show them onscreen, you can do something like this:

    for(UIButton *button in self.myButtons){
      //add the button to the view
      [self.view addSubview:button];
    }
    

    Of course, just adding buttons to the screen is useless. You need to be able to make them do something. To add an action to a button, you can use:

    [button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDown];
    

    The first part, addTarget:self, says that this view controller handles the event that you're going to ask it to handle. action:@selector(someMethod:) tells the class what method to perform when the event occurs. Then, forControlEvents:UIControlEventTouchDown says that the said class should perform the said method when the button is tapped.

    So, in your header:

    #import <UIKit/UIKit.h>
    
    @interface MyClass :UIViewController{
    
       NSMutableArray *myButtons; 
    }
    
    @property (nonatomic, retain) NSMutableArray *myButtons;
    
    //Use UIButton as the sender type if you want 
    - (void)someMethod:(id)sender;
    
    // ... Other code can go here of course
    @end
    

    And in your implementation, you can use this:

    #import MyClass.h
    
    
    @implementation MyClass
    
    - (id)init{
    
      self = [super init];
    
      if(self){
    
         NSMutableArray *temp = [[NSMutableArray alloc] init];
         self.myButtons = temp;
         [temp release];
    
      }
    
      return self;
    
    }
    
    
    - (void)viewDidLoad{
    
      //
      // Create the buttons
      //
    
      for(int i=0;i<numberOfButtons;i++){
        //Create the button
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,y,width, height)];
    
         //You may want to set button titles here
         //or perform other customizations
    
        //Store the button in our array
        [self.myArray addObject:button];
        //Release the button (our array retains it for us)
        [button release];
      }  
    
      //
      // Show the buttons onscreen
      //
    
      for(UIButton *button in self.myButtons){
        //add the button to the view
        [self.view addSubview:button];
        //add the action
        [button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDown];
      }
    }
    
    - (void)someMethod:(id)sender{
       //Do something when the button was pressed;
    }
    
    - (void)dealloc{
      [self.myButtons release];
      [super dealloc];
    }
    
    @end
    

    Now, you can go to button paradise without taking Interface Builder on the plane. Go UIButton happy!

    0 讨论(0)
  • 2021-01-02 11:19

    Key Value programming is your friend

    Basically you can loop around and create your buttons, and as long as you have properties defined for your buttons you can.

    [self setObject:newButton forKey@"button1"];
    

    where button1 is the same string as your variable name.

    0 讨论(0)
  • 2021-01-02 11:20

    Like so:

    NSMutableArray *arr = [NSMutableArray array];
    for(int i = 1; i <= 39; ++i) {
      [arr addObject:[self valueForKey:[NSString stringWithFormat:@"btn%d", i]]];
    }
    
    0 讨论(0)
  • 2021-01-02 11:25

    You could create a getter method named after each button and than call [self valueForKey:]; for each but something here just screams "terrible design" :)

    0 讨论(0)
  • 2021-01-02 11:41
    for(int i=1; i<totalButtonCount; i++) {
       NSString *buttonClassName = [NSString stringWithFormat:@"btn%d", i];
       [[NSClassFromString(buttonClassName) alloc] init];
       //add to array
    }
    

    Edit: after re-reading your question, this might not be what you're asking. I thought you wanted to create a bunch of instances of similar classes.

    0 讨论(0)
提交回复
热议问题