【发布时间】:2012-11-05 06:55:57
【问题描述】:
在我看来,我有 12 个按钮,一个数组包含 6 个名称,我想在 UIButton 标题中打印数组名称。这是我的代码:
texts = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil];
UIButton *button;
NSString *name;
NSUInteger count = [texts count];
int i=0;
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UIButton class]])
{
button= (UIButton *)view;
if(button.tag >= 1||button.tag <= 20)
{
int value = rand() % ([texts count] -1) ;
int myTag= i+1;
button = [self.view viewWithTag:myTag];
name=[NSString stringWithFormat:@"%@",[texts objectAtIndex:value]];
[button setTitle:name forState:UIControlStateNormal];
NSLog(@"current name :%@",name);
}
i++;
}
}
[super viewDidLoad];
我面临的问题是:
在重复洗牌时,我尝试使用What's the Best Way to Shuffle an NSMutableArray?,但它不起作用。
我想要 12 个按钮中的 6 个标题,这意味着每个标题将位于 2 个按钮中。请帮我解决这个问题。我应该做出哪些改变?
【问题讨论】:
-
1.您的数组中有 8 个标签,而不是 6 个。您从数组中随机选择一个标题,但没有从选项列表中删除该标题,因此您可以获得重复项。
-
@futureelite7 我应该做些什么改变来从选择列表中删除标题,
-
你想做什么?我不是 100% 确定我理解你的问题。
-
我想在 12 个按钮中显示 6 个标题(我更新任务),每个标题在 2 个按钮中,随机顺序 @futureelite7
-
@futureelite7 我认为如果您解释如何从数组中删除项目以及可能为什么需要这样做会很有帮助,我相信这将是一个公认的答案:)
标签: iphone ios uibutton nsmutablearray