2014-09-12 62 views
1

我有3個視圖控制器。第一種模式是從我的根視圖控制器中提出的。如何從UIAlertView按鈕退出segues

他們在使用push從第一到第三的導航堆棧中。

我最後的視圖控制器(第三)有一個UIAlertView,並按下好的時候,我想展開到第一個視圖控制器。

但是,我讀過關於這種展開的任何內容都暗示了-(IBAction),這是系統從故事板按鈕中激活的。

我如何從第三個視圖控制器放鬆回來?我用

[self performSegueWithIdentifier:segueString sender:self]; 

,但它崩潰就試圖瀏覽再次

+0

使用[self.navigationcontroller poptorootviewcontroller]方法 – 2014-09-12 10:22:40

+0

拯救生命。謝謝 – invisibloom 2014-09-12 10:24:28

回答

1

所以你需要如下使用UIAlertView中委託方法的應用,

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    //Using buttonIndex you could find whether OK or CANCEL is pressed and perform your popToRootViewController call. 
    //Below on OK press then moving to root VC 
    if (buttonIndex==0) 
    { 
    //OK is pressed... 
    [self.navigationController popToRootViewControllerAnimated:YES]; 
    } 
    else{ 
    //CANCEL is pressed... 
    } 
} 

希望這能幫助你。

0
[self.navigationController popToRootViewController] 

將幫助你解決你的問題

0

@ walle84給出的解決方案非常棒...但不是Unwind Segue解決方案。如果你想使用真正的Unwind Segues,請按照這個步驟進行。

在你FirstViewController.m文件,添加一個-reset:方法,使您的代碼如下所示:

#import "FirstViewController.h" 

@interface FirstViewController() 

@end 

@implementation FirstViewController 

-(IBAction)reset:(UIStoryboardSegue *)segue { 
    //you can do stuff here, if necessary 
    //popToRootViewControllerAnimated doesn't allow it 
    NSLog(@"Back to FirstViewController"); 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

@end 

在你ThirdViewController.m,設置您的-alertView:clickedButtonAtIndex:方法,使您的代碼如下所示:

#import "ThirdViewController.h" 

@interface ThirdViewController() <UIAlertViewDelegate> 

@end 

@implementation ThirdViewController 

- (IBAction)buttonPressed:(id)sender { 
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"MyApp" 
                 message:@"Do you want to reset to FirstViewController?" 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:nil]; 

    [message addButtonWithTitle:@"Perform Unwind"]; 
    [message show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 1) { 
     [self performSegueWithIdentifier:@"myUnwindSegue" sender:self]; 
    } 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

@end 

然後,在你的故事板,選擇ThirdViewController場景,點擊Third View Controller按鈕,將其拖動到Exit按鈕(見下圖)。彈出窗口會出現。選擇其中的Reset:鏈接。

enter image description here

完成後,選擇您unwind segue按鈕(見下圖),並在屬性檢查器,設置它的名稱爲「myUnwindSegue」(見下圖)。

enter image description here

如果您想了解更多關於放鬆身心塞格斯,你可以看WWDC 2012會話視頻「在您的應用程序採用故事板」 [開始在38分鐘]。