0

这里我的代码显示了一个图像视图的 4 个按钮标题,如果单击的按钮标题等于 UIImageview 图像,我想显示单击的警报。这是我的代码,

 imageary=[[NSMutableArray alloc]initWithObjects:@"dear.jpg",@"donkey.jpg",@"elephant.jpg",@"fox.jpg",@"giraffe.jpg",@"goat.jpg",@"buffallo.jpg",@"bull.jpg",@"cow.jpg",@"crocodile.jpg", nil]; // declare array of names not images
 - (NSString *) convertToDisplayName:(NSString *)actual
 {
  return [actual stringByDeletingPathExtension]; //return image name without extension
 }

按钮方法

-(IBAction)methodset:(id)sender
{

int i=0;
for(UIView *view in self.view.subviews)
{
  if([view isKindOfClass:[UIButton class]])
  {  
   mybutton = (UIButton *)view;
   if(mybutton.tag == 1||mybutton.tag == 2||mybutton.tag == 3||mybutton.tag == 4)
   {
     i = rand() % [imageary count]; // set random image
     animage.image=[UIImage imageNamed:[imageary objectAtIndex:i]]; 
    name=[self convertToDisplayName:[imageary objectAtIndex:i]];
    [mybutton setTitle:name forState:UIControlStateNormal];
    NSLog(@"current image name :%@",name);
    i++;

   }
  }
 }
}

现在我面临问题

  1. rand(),同时重新洗牌按钮中的一些值正在重复。如何在不重复的情况下调整值并在 UIButton 中显示。
  2. 我应该在哪里比较图像名称和单击的按钮标题是否相等。请帮我解决这个问题。请只做那些需要的。
4

3 回答 3

0

对 rand 位使用 while 循环

while (i != mybutton.tag){
   i = rand() % [imageary count];
}

只需比较 if(mybutton.tag ...etc 等之后的图像名称和 uibutton 标题,因为您希望它在 for 循环中对吗?还缩进您的代码 :P. 使其更易于阅读。

于 2012-11-08T07:43:17.117 回答
0
if ([btn.titleLabel.text isEqualToString:@"imageName"])  

根据你的代码

if ([btn.titleLabel.text isEqualToString: name])  
于 2012-11-08T07:46:14.097 回答
0

您可以通过更改以下功能来简单地做到这一点:

-(IBAction)methodset:(id)sender
{
  UIButton *mybutton = (UIButton *)sender;
  if(mybutton.tag == 1||mybutton.tag == 2||mybutton.tag == 3||mybutton.tag == 4)
  {
     for(int loop = 0; loop<4;loop++)
     {
        animage.image=[UIImage imageNamed:[imageary objectAtIndex:i]]; 
        name=[self convertToDisplayName:[imageary objectAtIndex:i]];
        [mybutton setTitle:name forState:UIControlStateNormal];
        NSLog(@"current image name :%@",name);
        if ([mybutton.titleLabel.text isEqualToString:[imageary objectAtIndex:i]])
        {
             //titles equal
        }

        UIView *newView = [self.view viewWithTag:i];
        if([newView isKindOfClass:[UIButton class]])
        {
           UIButton *newButton = (UIButton *)newView;
           [newbutton setTitle:name forState:UIControlStateNormal];
        }
     }
  }
}
于 2012-11-08T08:10:45.820 回答