在UI按钮中分配随机标题

时间:2012-11-10 10:16:44

标签: iphone ios uiimageview uibutton

我的application与测验相关,在我的视图中我有1张图片视图和4张UIbuttons,最后我需要在imageview中加载图片并分配4个按钮标题改组方式,其中一个按钮应该是当前图像名称,如果用户点击按钮我想显示下一组按钮标题和新image

 -(IBAction)answersetting:(id)sender
{
int i=0;
UIButton *mybutton = (UIButton *)sender;
[mybutton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
if(mybutton.tag == 1||mybutton.tag == 2||mybutton.tag == 3||mybutton.tag == 4)
{
           for(int loop = 0; loop<4;loop++)
    {
        animage.image=[UIImage  imageNamed:[NSString stringWithFormat:@"%@.jpg",[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
            [self alertshow];
            i++;
        }

        UIView *newView = [self.view viewWithTag:i];
        if([newView isKindOfClass:[UIButton class]])
        {
            UIButton *newButton = (UIButton *)newView;
            [newButton setTitle:name forState:UIControlStateNormal];
        }
    }
   }
 }

这无法执行我在上面描述的内容。我应该在代码中做出哪些更改?请帮助解决此问题

2 个答案:

答案 0 :(得分:1)

只需创建一个显示图像和设置按钮标题的方法。并使用另一种方法来评估猜测结果。

//This method display the image and buttons

-(void)displayImageAndButton:(int)pos
{
       animage.image=[UIImage  imageNamed:[NSString stringWithFormat:@"%@.jpg",[imageary objectAtIndex:pos]]];

        UIView *newView = [self.view viewWithTag:buttonTag1];
        if([newView isKindOfClass:[UIButton class]])
        {
            UIButton *newButton = (UIButton *)newView;
            name=[self convertToDisplayName:[imageary objectAtIndex:pos++]];
            [newButton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
            [newButton setTitle:name forState:UIControlStateNormal];
        }
        newView = [self.view viewWithTag:buttonTag2];
        if([newView isKindOfClass:[UIButton class]])
        {
            UIButton *newButton = (UIButton *)newView;
            name=[self convertToDisplayName:[imageary objectAtIndex:pos++]];
            [newButton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
            [newButton setTitle:name forState:UIControlStateNormal];
        }
        newView = [self.view viewWithTag:buttonTag3];
        if([newView isKindOfClass:[UIButton class]])
        {
            UIButton *newButton = (UIButton *)newView;
            name=[self convertToDisplayName:[imageary objectAtIndex:pos++]];
            [newButton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
            [newButton setTitle:name forState:UIControlStateNormal];
        }
        newView = [self.view viewWithTag:buttonTag4];
        if([newView isKindOfClass:[UIButton class]])
        {
            UIButton *newButton = (UIButton *)newView;
            name=[self convertToDisplayName:[imageary objectAtIndex:pos++]];
            [newButton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
            [newButton setTitle:name forState:UIControlStateNormal];
        }
 }

    //this method evaluates the answer

    - (IBAction)submitGuess:(id)sender
    {
      UIButton *mybutton = (UIButton *)sender;
      if ([mybutton.titleLabel.text isEqualToString:[imageary objectAtIndex:positionOfQuest]])
      {
                //titles equal
                [self alertshow];
      }
      positionOfQuest++;
      [self displayImageAndButton:positionOfQuest];
    }

这里有buttonTag1,buttonTag2,buttonTag3&amp; buttonTag4是按钮的标签。

int viewDidLoad声明一个变量,如:

static positionOfQuest = 0; //this for holding the question number

在IB中添加按钮标签并在调用以下方法之前在viewDidLoad中初始化它们

你需要在viewDidLoad中调用方法,如:

[self displayImageAndButton:positionOfQuest];

答案 1 :(得分:0)

如果你喜欢简单的方法......

#include <stdlib.h>

NSMutableArray *quiz = [NSMutableArray alloc....];

for (int i=0; i<4; i++) { 
   int r = arc4random() % [quiz count];

   NSLog(@"%@", [quiz objectAtIndex:r]);
  _Button_1.title = [quiz objectAtIndex:r];

  // or better, i suggest you to use KVC
  // [self valueForKey:@"_Button_%d", i] setTitle //something like this.
  //
}