簡體   English   中英

在UIbutton中分配隨機標題

[英]Assign random title in UIbutton

我的application類似於測驗,在我的視圖中,我具有1個圖像視圖和4個UIbuttonsUIbuttons我必須在imageview加載圖像並以UIbuttons方式分配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];
        }
    }
   }
 }

這無法說明我在上面描述的內容。我應該對代碼進行哪些更改?請幫助解決此問題

只需創建一種顯示圖像和設置按鈕標題的方法即可。 並使用另一種方法評估猜測結果。

//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是按鈕的標簽。

viewDidLoad聲明一個變量,例如:

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

在IB中,添加按鈕標簽並在調用以下方法之前在viewDidLoad對其進行初始化

並且您需要像這樣在viewDidLoad調用該方法:

[self displayImageAndButton:positionOfQuest];

如果您喜歡簡單的方法...

#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.
  //
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM