【问题标题】:Create a for loop to add 39 buttons to an array创建一个 for 循环以将 39 个按钮添加到数组中
【发布时间】:2011-08-05 02:43:31
【问题描述】:

我的 .h 文件中有 39 个不同的 UIButton 变量,但我想将它们中的每一个都添加到一个数组中,而不必输入相同的内容 39 次。

有没有办法在 for 循环中做到这一点?

相应的按钮命名为:btn1、btn2、btn3 等。

【问题讨论】:

    标签: iphone objective-c xcode loops uibutton


    【解决方案1】:

    您可能希望放弃头文件中的 39 个按钮,而使用单个数组。 我怀疑您想使用手动引用,以便可以利用 Interface Builder 来控制事件和布局。我建议做一些不同的事情。

    创建一个属性 - NSMutableArray。然后,当视图加载时,动态创建按钮。

    要访问按钮,请使用 [self.arrayOfButtons objectAtIndex:38]; 之类的内容。 (如果有 39 个按钮,那将返回最后一个按钮。);`

    要创建按钮,请使用以下内容:

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

    请注意,您传入按钮的 init 方法的框架。您的按钮框架将从其容器的左上角开始,您的按钮将是 100 像素正方形。框架是CGRect 的一个实例。 (您通过调用函数CGRectMake(x,y,width,height) 创建一个CGRect

    要制作 39 个按钮,您可能需要如下循环,给定一个数组来保存按钮,myButtons 和预定义的 numberOfButtons 和维度变量:

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

    当然,您需要为每个按钮设置 xywidthheight 的唯一值,否则它们将全部重叠。创建按钮后,您可以对它们进行操作,例如设置标签或在屏幕上显示它们。要在屏幕上显示它们,您可以执行以下操作:

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

    当然,只是在屏幕上添加按钮是没有用的。你需要能够让他们做一些事情。要向按钮添加操作,您可以使用:

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

    第一部分,addTarget:self,表示此视图控制器处理您将要求它处理的事件。 action:@selector(someMethod:) 告诉类在事件发生时执行什么方法。然后,forControlEvents:UIControlEventTouchDown 表示当点击按钮时,该类应该执行该方法。

    所以,在你的标题中:

    #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
    

    在你的实现中,你可以使用这个:

    #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
    

    现在,您无需在飞机上携带 Interface Builder 即可前往按钮天堂。祝 UIButton 快乐!

    【讨论】:

    • 多么绝妙的方法啊!!我正在使用 UIScrollView,因此设置框架最初会更加困难。非常感谢您花时间在这上面,非常感谢!
    • 谢谢。实际上,在 UIScrollView 中设置框架并不难。 UIScrollView 有一个名为 contentSize 的属性。框架基于父视图的框架。只需将它们添加到滚动视图即可。
    • 另外,objective-c中不存在foreach方法。
    • 尝试发布一个新问题和示例代码,显示您尝试过的内容和无效的内容 b
    【解决方案2】:

    像这样:

    NSMutableArray *arr = [NSMutableArray array];
    for(int i = 1; i <= 39; ++i) {
      [arr addObject:[self valueForKey:[NSString stringWithFormat:@"btn%d", i]]];
    }
    

    【讨论】:

      【解决方案3】:
      for(int i=1; i<totalButtonCount; i++) {
         NSString *buttonClassName = [NSString stringWithFormat:@"btn%d", i];
         [[NSClassFromString(buttonClassName) alloc] init];
         //add to array
      }
      

      编辑:重新阅读您的问题后,这可能不是您要问的。我以为你想创建一堆类似类的实例。

      【讨论】:

      • Scheriman 有没有可能是我在编辑之前写了评论? :)
      • 我马上就成功了。请原谅我之前的语气,昨天是……压力很大。 ;)
      • 不用担心。无论如何,为了记录,我在你做出答案后很快就回复了,并且正在用我的 iPhone 打字,所以也许在我完成打字之前我什至看不到编辑;p 是的,rep-whoring 是目前为止的一些类型作为 iPhone 的 SO'ing 呵呵 :)
      【解决方案4】:

      您可以创建一个以每个按钮命名的 getter 方法,然后调用 [self valueForKey:];对于每一个但这里的东西只是尖叫“糟糕的设计”:)

      【讨论】:

      • 完全同意。有些东西很臭。
      【解决方案5】:

      Key Value programming是你的朋友

      基本上,您可以循环创建按钮,只要您为按钮定义了属性即可。

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

      其中 button1 是与变量名称相同的字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-19
        • 1970-01-01
        • 2019-08-28
        • 1970-01-01
        • 2012-09-21
        • 2021-04-22
        • 2020-10-22
        • 1970-01-01
        相关资源
        最近更新 更多