2013-01-17 128 views
6

我有一個使用故事板的應用程序。在視圖中有一個AlertViewDialog。手動打開故事板視圖

當用戶單擊第一個按鈕(「是」)時,如何打開Storyboard上的其他視圖?

回答

10

我的是這可以幫助:

  1. 將在隨後視圖去身份檢查(快捷鍵:選項+蘋果+ 3)。
  2. 選擇標題爲故事板ID的標識檢查器中新拖動的視圖並給出唯一名稱。 //查看該圖像爲基準 enter image description here

創建SecondViewController類(.H & .M)的viewController的子類。

然後從警報視圖代碼(如你點擊YES時說的)

粘貼下面

SecondViewController *svc =[self.storyboard instantiateViewControllerWithIdentifier:@"vinay"]; 
     [svc setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; 
     [self presentViewController:svc animated:YES completion:nil]; 

提到的代碼讓我知道如果出現任何問題。

+0

謝謝!有用! –

+0

您的歡迎和感謝喜歡我的文章:) –

5

願這有助於:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
ClassNameViewController *viewController = (ClassNameViewController *)[storyboard instantiateViewControllerWithIdentifier:@"viewIdentifierOnStoryboard"]; 
[self presentModalViewController:viewController animated:NO]; 
+0

太糟糕了,不起作用。什麼都沒有發生...... –

+0

您是否在視圖上設置了標識符並使​​用了相同的故事板名稱? – Tchelow

+0

我已將Storyboard ID設置爲'eerstekeer'。選中「使用故事板ID」,導入啓動。h(classname)並使用此代碼: UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@「MainStoryboard」bundle:nil]; startup * viewController =(startup *)[storyboard instantiateViewControllerWithIdentifier:@「eerstekeer」]; –

0

你需要做的第一件事就是設置UIAlertView委託做這個添加UIAlertViewDelegate@interface所以它看起來像

 @interface myClass : super <UIAlertViewDelegate> 
      // super could be anything like `UIViewController`, etc 
     @end 

,並在@implementation可以再補上一句

 @implementation myClass 

     ........... Some code 


     - (IBAction)someActionMethod:(id)sender 
     { 
      UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:nil 
                    message:@"Would you like to move on?" 
                   delegate:self 
                 cancelButtonTitle:@"No" 
                 otherButtonTitles:@"Yes", nil]; 
      [myAlertView show]; 
      // [myAlertView release]; Only if you aren't using ARC 
     } 

     - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
     { 
       switch(buttonIndex) { 
        case 1: 
          SecondViewController *svc =[self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"]; 
          [svc setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; 
     // [self presentViewController:svc animated:YES]; // Deprecated in iOS 6.0 
          [self presentViewController:svc animated:YES completion:nil]; // Introduced in iOS 5.0 
          break; 
        default: 
          break; 
       } 
     } 

     @end 

請記住在故事板中設置唯一標識符。您可以通過轉到.storyboard並在Identity Inspector(選擇第三個)中執行此操作,您可以設置Storyboard ID這是您需要與instantiateViewControllerWithIdentifier中的內容匹配的內容,因此在上面的情況下它將是"secondViewController"。這很簡單。

記住駁回這一觀點,當你做了你需要使用

 [self dismissModalViewControllerAnimated:YES]; 

上述內容已經實際的iOS 6.0被棄用,但你可以使用

 [self dismissModalViewControllerAnimated:YES completion:nil]; 

哪個做同樣的事情除了在結尾添加完成塊之外。

希望這會有所幫助。