【问题标题】:NSMutableArray UIButtons Action?NSMutableArray UIButtons 动作?
【发布时间】:2012-05-17 14:41:44
【问题描述】:

我正在以编程方式创建UIButtons,然后将这些UIButtons 存储在NSMutableArray 中。所有这些UIButtons 都有一些文本作为标题。我没有这些UIButtons 的出口。现在我想在触摸或单击某些UIButton 时将UIButton 的标题分配给UIlabel。但对我来说问题是:我怎样才能在这些UIButtons 上执行该功能。因为我以编程方式创建了UIButtons,而且这些UIButtons 没有插座。这是我创建按钮的代码:

     saveBtn = [[NSMutableArray alloc] init];

    for (int i=0; i<30; i++) {


        if (btnn>8) {
             UIButton *btn = [UIButton   
            buttonWithType:UIButtonTypeRoundedRect];
            btn.frame = CGRectMake(200.0 , spacey, 30.0, 30.0);
            int idx;
            idx = arc4random()%[arr count];
            NSString* titre1 = [arr objectAtIndex:idx];
            [btn setTitle:titre1 forState:UIControlStateNormal];      
            spacey=spacey+30;
            spacex = 80;
            btnn = 0;
            }
        else {
            btnn++ ;
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn.frame = CGRectMake(spacex, spacey, 30.0, 30.0);
            int idx;
            idx = arc4random()%[arr count];
            NSString* titre1 = [arr objectAtIndex:idx];
            [btn setTitle:titre1 forState:UIControlStateNormal];   
            spacex = spacex + 30;
            [saveBtn addObject:btn];
            [self.view addSubview:btn];

        }
    }

请任何人都可以指导我如何为这些NSMutableArray UIButtons 执行操作。

【问题讨论】:

    标签: iphone objective-c ios uibutton nsmutablearray


    【解决方案1】:

    您可以使用以下方式以编程方式向 UIButton 添加操作:

    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    - (void)buttonClicked:(UIButton *)btn
    {
        [btn setTitle:@"UnAutreTitre" forState:UIControlStateNormal];
    }
    

    希望这会有所帮助。

    【讨论】:

    • 亲爱的我试了一下..thanx 但我的按钮存储在数组中,所以在这里我们如何提供我的 Array.bcz 的参考我想对存储在我的数组中的 uibutton 执行操作。跨度>
    • 你知道,有 [NSArray objectAtIndex:]... 说真的,请阅读文档以了解诸如“如何访问数组中的第 n 个元素”之类的基本问题...
    • 谢谢亲爱的你的代码对我来说是完美的。当我在数组中添加 UIButtons 之前实现它..它工作正常。所以谢谢你我用你的代码解决了我的问题。
    【解决方案2】:

    只需设置标签并将目标添加到按钮。稍后,您可以获取点击了哪个按钮。

    【讨论】:

      【解决方案3】:

      UIButton 操作创建操作,您将在其中将(id)sender 发送到actionButtonFunction

      将发件人分配给UIButton,然后取NSString中的按钮标题。

      -(UIAction)buttonAction:(id)sender
      {
          UIButton *button = (UIButton *)sender;
          NSString *buttonTitle = button.text;
      
          // further code goes here
      }
      

      【讨论】:

      • thanx 但我的按钮存储在数组中,所以在这里我们如何提供我的 Array.bcz 的引用,我想对数组中的 uibutton 执行操作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多