0

我會從UIAlertView按鈕打開一個UIViewController,但它不起作用。從UIAlertView按鈕以編程方式推送視圖?

當用戶終止比賽,我表現出一些文字和兩個按鈕UIAlertView「確定」來關閉提醒和「分數」打開比分頁。

這是實際UIAlertView

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"YOU WIN!" 
                  message:[NSString stringWithFormat:@"You win a game in %d seconds!",seconds] 
                  delegate:self 
                cancelButtonTitle:@"Ok" 
                otherButtonTitles:@"Score", nil]; 
     [alert show]; 

,這是代碼推的觀點:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 
    if([title isEqualToString:@"Score"]){ 
     ScoreViewController *score = [[ScoreViewController alloc]init]; 
     [[self navigationController] pushViewController:score animated:YES]; 
    } 
} 

我得到是一個完全黑屏。有人能幫我嗎?我無法弄清楚我做錯了什麼。

PS:ScoreViewController從根視圖中有一個segue,但我當然不能從故事板創建一個新的,因爲我想通過編程方式從alert view按鈕執行segue。

希望我已經清楚,任何幫助將非常感激!

+1

它看起來並不像是在執行segue。以編程方式執行Storyboard segue的方法是'performSegueWithIdentifier:sender:' – hgwhittle

+0

ops對不起,您是對的!我之前嘗試過使用segue,但沒有工作,所以我搜索並找到'pushViewController:animated:',然後我忘記更改代碼。我將編輯我的問題,但'執行SegueWithIdentifier:發件人:'使應用程序崩潰與'線程1:信號SIGABRT' –

回答

0

看起來你正在實例化一個ScoreViewController的實例並推送到你的導航控制器。雖然這是呈現另一個控制器的合法方式,但它與執行Storyboard segue不同。

首先,顯然,您必須確保在故事板中有一個連接視圖控制器的segue。這聽起來像你已經做到了這一點,所以接下來你要確保segue有一個標識符。選擇segue並在Attributes檢查器中設置它的標識符。然後在代碼中執行segue:

[self performSegueWithIdentifier:@"YourIdentifier" sender:self]; 
+0

我忘了「明顯」的一部分!大聲笑非常感謝你的幫助。 –