2014-09-12 83 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分钟]。