0

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

这无法执行我上面描述的内容。我应该对我的代码进行哪些更改?请帮助解决这个问题

4

2 回答 2

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 & buttonTag4 是您的按钮的标签。

intviewDidLoad声明一个变量,如:

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

在 IB 中添加按钮标签并viewDidLoad在调用以下方法之前对其进行初始化

你需要viewDidLoad像这样调用该方法:

[self displayImageAndButton:positionOfQuest];
于 2012-11-17T07:15:30.293 回答
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.
  //
}
于 2012-11-17T07:42:28.700 回答