2012-03-11 57 views
1

我創建一個測驗應用程序,我想知道使用多個,而不是if語句,我可以做一個使用INT之間交替算法UIViews當您單擊一個UIButton。隨着多個if語句代碼將是這個樣子:創建算法從按下一個UIButton更改的UIView

IBAction NextQuestion; { 

    Question++; //QUESTION IS THE int FOR THE ALGORITHM. 

    if (Question==2) { 
     self.view = PlayView2; //PlayView2 is the UIView I'm switching to. 
     } 

    if (Question==3) { 
     self.view = PlayView3; //PlayView3 is the UIView I'm then switching to after the next object/UIButton is pressed. 
     }   

等等...

IBAction爲連接到多個UIButtons更改視圖。目標是我想改變UIView算法而不是多個如果語句感謝您的幫助!

+1

如何將你的意見到一個數組,並得到0和array.count-1之間的隨機數?然後你可以得到你的視圖:'View = [array objectAtIndex:randomNumber];'。沒有,如果聲明需要任何。 – Jeremy 2012-03-11 15:57:59

回答

1
myViewsArray = [[NSArray alloc] initWithObjects: PlayView2, PlayView3, ..., PlayViewN, nil]; 

    -(UIView*) nextView { 
     if(Question < [myViewsArray count]) 
      return [myViewsArray objectAtIndex: Question++]; 

     return nil; 
    } 
+0

謝謝!似乎很棒。 – 2012-03-11 16:34:57

0

可能是開關盒?

switch(Question) { 
    case 2: self.view = PlayView2; 
      break; 
    case 3: self.view = PlayView3; 
      break; 
} 
相關問題