【问题标题】:How to unwind segues from UIAlertView button如何从 UIAlertView 按钮中解除 segues
【发布时间】:2014-09-12 10:20:13
【问题描述】:

我有 3 个视图控制器。第一个是从我的根视图控制器模态呈现的。

它们位于使用push 从第一到第三的导航堆栈中。

我的最终视图控制器(第三个)有一个UIAlertView,按确定后,我想放松到第一个视图控制器。

但是,我所读到的有关这种展开的任何内容都表明-(IBAction),这是系统通过情节提要按钮激活的。

如何从第三个视图控制器放松到第一个视图控制器?我用过

[self performSegueWithIdentifier:segueString sender:self];

但它在尝试再次导航时使应用程序崩溃

【问题讨论】:

  • 使用[self.navigationcontroller poptorootviewcontroller]方法

标签: ios objective-c xcode uistoryboardsegue


【解决方案1】:

所以你需要使用 UIAlertView Delegate 方法如下,

- (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...
  }
}

希望对您有所帮助。

【讨论】:

    【解决方案2】:
    [self.navigationController popToRootViewController] 
    

    将帮助您解决问题

    【讨论】:

      【解决方案3】:

      @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
      

      然后,在您的 Storyboard 中,选择您的 ThirdViewController 场景,单击 Third View Controller 按钮并将其拖动到 Exit 按钮(见下图)。将出现一个弹出窗口。选择其中的Reset: 链接。

      完成后,选择您的 unwind segue 按钮(见下图),然后在 Attributes Inspector 中将其名称设置为“myUnwindSegue”(见下图)。

      如果您想了解更多关于 Unwind Segues 的信息,可以观看 WWDC 2012 会议视频“在您的应用中采用故事板”[开始于 38 分钟]。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-03
        相关资源
        最近更新 更多