如何检查单击的按钮标题是否等于uiimageview中的图像名称 [英] How to check the clicked button title is equal to the image name in uiimageview

查看:41
本文介绍了如何检查单击的按钮标题是否等于uiimageview中的图像名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here my code to show the 4 button titles which one image view, I want to show the clicked alert if clicked button title is equal to the UIImageview image. Here is my code,

 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. 我应该在哪里比较图像名称和单击的按钮标题是否相等.请帮助我解决此问题.请做需要.

推荐答案

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

You can simply do it by changing the function like:

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

这篇关于如何检查单击的按钮标题是否等于uiimageview中的图像名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆