0

applicationはクイズに関連するようなものです,私のビューでは1つの画像ビューと4つUIbuttonsがあります,最初に画像をロードしてimageview4つのボタンタイトルをシャッフル方法で割り当てる必要があります,ボタンの1つは現在の画像名にする必要があります,ユーザーがクリックした場合ボタン 次のボタンのタイトルと新しいセットを表示したい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 はボタンのタグです。

次のような変数をint でviewDidLoad宣言します。

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 に答える