2015-04-01 78 views
0

我想知道如何從UIAlertView開始一個新的ViewController。對我來說,你似乎無法用圖形方式連接viewControllers的方式,你可以用一個按鈕。從UIAlertView開始一個新的ViewController

我已在UIAlertView中成立了我的第一次的viewController像這樣:

UIAlertView *messageAlert = [[UIAlertView alloc] 
           initWithTitle:@"Start New View Controller?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes",nil]; 

然後我處理這樣的不同的按鈕點擊這樣:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex{ 
if (buttonIndex == 0){ 
    NSLog(@"Cancel"); 
} 
if (buttonIndex == 1){ 
    NSLog(@"Yes"); 
    } 
} 

我將如何啓動第二的viewController(比方說viewContoller2)與這個alertbox?

回答

0

您可以以常規方式啓動視圖控制器,也可以使用segue。對於SEGUE,你會做這樣的事情:

if (buttonIndex == 1){ 
    [self performSegueWithIdentifier:@"SEGUE_IDENTIFIER" sender:self]; 
} 
代碼

以後,您設置控制器是這樣的:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"SEGUE_IDENTIFIER"]) { 
     MyViewController *viewContoller2 = (MyViewController *)segue.destinationViewController; 
     NSLog("viewContoller2 is ready for use"); 
    } 
} 

或不塞格斯,做到:

if (buttonIndex == 1){ 
    MyViewController *viewContoller2 = [[MyViewController alloc] init]; 
    [self presentViewController:viewContoller2 animated:YES completion:nil]; 
} 
+0

請注意,自從4月1日以來,我一直保留「viewContoller2」及其顯而易見的錯字。 :) – Rikkles 2015-04-02 00:03:49

+0

更像第一次世界無政府狀態! – 2015-04-02 00:24:23