2012-07-19 116 views
9

我有一個UIButton,我試圖將它連接到兩個segues。使用哪個賽格取決於許多條件。ios - 將一個UIButton連接到2段

例如,當的UIButton(標題爲 「下一個」)時,

如果(條件1),然後進入到屏幕A.別人去篩選B.

我相信我的代碼是正確的(反正我已經包括了它),但問題是在故事板視圖中,我可以使用segue將一個按鈕連接到僅一個視圖控制器。我該如何解決 ?

下面是我使用的代碼 -

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
     if([[segue identifier]isEqualToString:@"nextButtonID"]){ 
      creditCardViewController *cvc = [segue destinationViewController]; 
     } 
     else if([[segue identifier]isEqualToString:@"next2ButtonID"]){ 
      depositInfoViewController *divc = [segue destinationViewController]; 
     } 
} 

-(IBAction)nextClicked{ 
     if([accountType isEqualToString:@"patron"]){ 
      [self performSegueWithIdentifier:@"nextButtonID" sender:self]; 
     } 
     else{ 
      [self performSegueWithIdentifier:@"next2ButtonID" sender:self]; 
     } 
} 

回答

17

在故事板窗口,從該視圖底部的視圖控制器圖標調速拖動,並將其拖動到目標視圖控制器,給SEGUE的標識符,並重復下一個賽格。

+0

但它是從viewController,而不是從按鈕。如果我理解正確。 – 2014-02-19 11:00:41

+1

這是正確的。沒有任何賽段是「物理」連接到按鈕的。 – 2014-02-19 13:31:00

+0

故事板只讓我按ctrl +從它拖動一個segue。在一個segue擁有作爲視圖控制器的源代碼之後,我無法再讓該視圖控制器成爲新的segue的源代碼。這是新的嗎? – 2017-08-17 16:08:10

4

你,除非你希望將一些數據傳遞給其他視圖控制器

-(IBAction)nextClicked 
{ 
    if ([strCheck isEqualToString:@"launch Screen A"]) 
     { 
      UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; 

      ScreenA *screenA = (ScreenA *)[storyboard instantiateViewControllerWithIdentifier:@"ScreenA"]; 
      [self.navigationController pushViewController:screenA animated:NO]; 

     } 
     else 
     { 

      UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; 

      ScreenB *screenB = (ScreenB *)[storyboard instantiateViewControllerWithIdentifier:@"ScreenB"]; 

      [self.navigationController pushViewController:screenB animated:NO]; 

     } 
} 
1

我經常這樣做不需要提及任何代碼prepareForSegue。我的方法是製作兩個「手動」遊戲。

  • 在Interface Builder CTRL +拖動從視圖控制器1查看這個賽格瑞在IB控制器2
  • 點擊,進入屬性和名稱及其標識:nextButtonID
  • 創建第二SEGUE,並按照相同的步驟將其命名爲next2ButtonID

那麼你的代碼應該可以正常工作,我覺得。我遇到了一個問題,我從一個按鈕開始繼續 - 然後 - 我也試着調用不同的繼續。通過使這些通用的「控制器到控制器」的繼續,他們被設置爲手動調用,就像你正在做的那樣。

0

夫特3

在故事板,CTRL +拖動創建塞格斯從SourceViewControllerDestAViewControllerDestBViewController,並給每個原因請看的標識符(例如 「ToDestA」 和 「ToDestB」)。然後:

@IBAction func onButton(_ sender: Any) { 
    if (testForA){ 
     self.performSegue(withIdentifier: "ToDestA", sender: Any?) 
    } else { 
     self.performSegue(withIdentifier: "ToDestB", sender: Any?) 
     } 
    }