【问题标题】:Put Random UIButtons Title of NSMutableArray to UIButtons of NSMutablearray将 NSMutableArray 的随机 UIButtons 标题放入 NSMutablearray 的 UIButtons
【发布时间】:2012-05-15 07:54:56
【问题描述】:

我有一个带有 3 个 UIButtons 的 NSMutableArray 和一个带有 50 个 UIButtons 的第二个 NSMutableArray。我想将这 3 个 UIButton 标题逐个字符地分配给 SecondArray 的每个 UIButton。但是 Second Array 的这 50 个 UIButton 中的每一个都已经有一个 Character。我的基本目的是首先我想检查每个 UIButton 的长度我的第一个数组的 uibutton 标题,然后将其按索引分配给第二个数组中的 UIButton。(对于 Exp,如果我的第一个按钮有标题“堆栈”,第二个“结束”和第三个“流”不我想分配索引的 UIButtons 0-5 ,13-16,35-38 在第二个数组中)。我的第一个数组有 3 个 UIBttons 。这里是代码。

NSMutableArray *buttons = [NSMutableArray arrayWithObjects:btn1, btn2, btn3, nil];
            [btn1 setTitle:@"stack" forState:UIControlStateNormal]; 
            [btn2 setTitle:@"over" forState:UIControlStateNormal]; 
            [btn3 setTitle:@"flow" forState:UIControlStateNormal]; 

这是我的代码。创建 50 个 UIButton。

 -(void)button:(id)sender {
  saveBtn = [[NSMutableArray alloc] init];
  arr=[[NSArray alloc]
  initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",
     @"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];

    for (int i=0; i<50; i++) {
            btn = [UIButton   buttonWithType:UIButtonTypeRoundedRect];
            btn.frame = CGRectMake(spacex, spacey, 30.0, 30.0);
            int idx;
            idx = arc4random()%[arr count];
            NSString* titre = [arr objectAtIndex:idx];
            [btn setTitle:titre forState:UIControlStateNormal];   
            spacex = spacex + 30;
            [saveBtn addObject:btn];
            [self.view addSubview:btn];

        }


            }

任何人都指导我如何将我的 UIButton 标题分配给特定索引上的这些 UIButton。谢谢

【问题讨论】:

  • 我认为在事件处理程序方法-(void)button:(id)sender 中创建按钮数组不是一个好主意。还是这是故意的?您可以在之前创建按钮(在某种 init 或 loadView 方法中),并且只在事件处理程序中分配它们的标题。无论如何:至于您的主要问题:您能否详细说明您想要实现的目标?您可以使用[(UIButton *)[saveBtn objectAtIndex: index] setTitle:@"title" forState: UIControlStateNormal]; 指定具有特定索引的按钮标题。不确定这是否是您的问题:)
  • 我真的不明白你的问题......你正在添加 50 个随机标题从“A”到“Z”的按钮并将它们存储在 saveBtn 中,然后用不同的 x 坐标显示它们?有什么问题?
  • 亲爱的你对,但我想将这些随机标题“A”替换为“Z”,从那些我想分配我的第一个数组 uibutton 标题“stack”、“over”和“flow”的 uibutton在特定的索引上。我提到的问题。

标签: iphone objective-c ios uibutton nsmutablearray


【解决方案1】:

感谢帮助每个人..但我在这里完成的是代码。我可以帮助其他人。

 - (void)getbuttontitle {
   for(int i = 0;i<[buttons count];i++)
  {
     if(i == 0)
    {
     UIButton *b1 = [buttons objectAtIndex:0];
      a = (NSString *)[[b1 titleLabel] text];
        [storewords addObject:a];
        NSLog(@"b=%@",a);
        }
    else if(i == 1)
    {

    }
    else if(i == 2)
    { 

    }
 }
}

    for (NSString *str in storewords) {
        if (str==a) {
            int j = 0;
            int randn= (arc4random()%5)+1 ;
            NSLog(@"n=%d",randn);
            for (int i=randn; i<[a length]+randn; i++) {
                NSLog(@"a=%@",a);
                NSString *ichar  = [NSString stringWithFormat:@"%c", [a characterAtIndex:j]];
                UIButton*  b = [saveBtn objectAtIndex:i];
                NSLog(@"a=%@",ichar);
                j++;
                [b setTitle:ichar forState:UIControlStateNormal]; 
            }   
        }

        if (str==c) {


        }   

        if (str==d) {

        }       
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 2012-04-15
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多