ios 如何以编程方式执行 Unwind segue?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12509422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:16:13  来源:igfitidea点击:

How to perform Unwind segue programmatically?

iossegueios6

提问by tadasz

Using storyboard this is very easy. You just drag the action to "Exit". But how should I call it from my code?

使用故事板这很容易。您只需将操作拖到“退出”即可。但是我应该如何从我的代码中调用它?

采纳答案by Vadim

  1. Create a manual segue (ctrl-drag from File's Owner to Exit),
  2. Choose it in the Left Controller Menu below green EXIT button.
  1. 创建手动转场(ctrl-drag from File's Owner to Exit),
  2. 在绿色退出按钮下方的左侧控制器菜单中选择它。

Choose it in the Left Controller Menu below green EXIT button

在绿色退出按钮下方的左侧控制器菜单中选择它

Insert Name of Segue to unwind.

插入 Segue 的名称以展开。

Then,- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender.with your segue identify.

然后,- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender.用你的 segue 标识。

回答by smileBot

Here's a complete answer with Objective C and Swift:

这是 Objective C 和 Swift 的完整答案:

1) Create an IBActionunwind segue in your destination view controller (where you want to segue to). Anywhere in the implementation file.

1)IBAction在你的目标视图控制器中创建一个unwind segue(你想要转场的地方)。实现文件中的任何位置。

// Objective C

    - (IBAction)unwindToContainerVC:(UIStoryboardSegue *)segue {

    }

// Swift

 @IBAction func unwindToContainerVC(segue: UIStoryboardSegue) {

    }

2) On the source view controller (the controller you're segueing from), ? + drag from "Name of activity" to exit. You should see the unwind segue created in step 1 in the popup. (If you don't see it, review step one). Pick unwindToContainerVC: from the popup, or whatever you named your method to connect your source controller to the unwind IBAction.

2)在源视图控制器(您要从中分离的控制器)上,?+ 从“活动名称”拖动以退出。您应该会在弹出窗口中看到在步骤 1 中创建的 unwind segue。(如果没有看到,请查看第一步)。从弹出窗口中选择 unwindToContainerVC: 或任何您命名的方法,以将源控制器连接到 unwind IBAction。

enter image description here

在此处输入图片说明

3) Select the segue in the source view controller's document outline of the storyboard (it will be listed near the bottom), and give it an identifier.

3)在故事板的源视图控制器的文档大纲中选择segue(它将在底部附近列出),并给它一个标识符

enter image description here

在此处输入图片说明

4) Call the unwind segue using this method from sourceview controller, substituting your unwind segue name.

4) 使用此方法从视图控制器调用 unwind segue ,替换您的 unwind segue 名称。

// Objective C

// 目标 C

[self performSegueWithIdentifier:@"unwindToContainerVC" sender:self];

[self performSegueWithIdentifier:@"unwindToContainerVC" sender:self];

// Swift

// 斯威夫特

self.performSegueWithIdentifier("unwindToContainerVC", sender: self)

self.performSegueWithIdentifier("unwindToContainerVC", sender: self)

NB. Use the sourceViewController property of the segue parameter on the unwind method to access any exposed properties on the source controller. Also, notice that the framework handles dismissing the source controller. If you'd like to confirm this add a dealloc method to the source controller with a log message that should fire once it has been killed. If dealloc doesn't fire you may have a retain cycle.

注意。在 unwind 方法上使用 segue 参数的 sourceViewController 属性来访问源控制器上的任何公开属性。另外,请注意框架处理解除源控制器。如果您想确认这一点,请向源控制器添加一个 dealloc 方法,其中包含一条日志消息,该消息在它被杀死后应触发。如果 dealloc 没有触发,您可能有一个保留周期。

回答by Dean

bradleygriffith's answer was great. I took step 10 and made a screenshot for simplification. This is a screenshot in Xcode 6.

bradleygriffith的回答很棒。我采取了步骤 10 并制作了一个屏幕截图以进行简化。这是 Xcode 6 中的屏幕截图。

  1. Control-drag from the orange icon to the red Exit icon to create an unwind without any actions/buttons in the view.
  1. 按住 Control 键从橙色图标拖动到红色退出图标以在视图中创建没有任何操作/按钮的放松。

enter image description here

在此处输入图片说明

  1. Then select the unwind seguein the sidebar:
  1. 然后unwind segue在侧边栏中选择:

enter image description here

在此处输入图片说明

  1. Set a Segue Identifier string:
  1. 设置 Segue 标识符字符串:

enter image description here

在此处输入图片说明

  1. Access that identifier from code:
  1. 从代码访问该标识符:
[self performSegueWithIdentifier:@"unwindIdentifier" sender:self];

回答by Larry Gibson

I used [self dismissViewControllerAnimated: YES completion: nil];which will return you to the calling ViewController.

我使用[self dismissViewControllerAnimated: YES completion: nil];which will return you to the call ViewController

回答by Vishal Chaudhry

Quoting text from Apple's Technical Note on Unwind Segue: To add an unwind segue that will only be triggered programmatically, control+drag from the scene's view controller icon to its exit icon, then select an unwind action for the new segue from the popup menu.

引用 Apple 关于 Unwind Segue 的技术说明中的文本:要添加仅以编程方式触发的 unwind segue,请从场景的视图控制器图标控制+拖动到其退出图标,然后从弹出菜单中为新 segue 选择一个 unwind 操作。

Link to Technical Note

链接到技术说明

回答by Jason Crocker

Vishal Chaudhry's answer above worked for me. I would also add that in order to manually trigger the seque using:

Vishal Chaudhry 上面的回答对我有用。我还要补充一点,以便使用以下方法手动触发序列:

[self performSegueWithIdentifier:@"mySegueName" sender:self];

from within the ViewController you must also select the unwind segue under the ViewController's Scene in the storyboard and in the properties view on the RHS ensure that the Indentifier field contains the namer you're referring to in the code ("mySegueName" in the example above).

在 ViewController 中,您还必须选择情节提要中 ViewController 场景下的展开转场,并在 RHS 的属性视图中确保 Indentifier 字段包含您在代码中引用的名称(上例中的“mySegueName”) )。

If you omit this step, the line above will throw an exception that the seque name is not known.

如果你省略这一步,该线之上会抛出异常的塞克名字不知道。

回答by Joel Teply

Backwards compatible solution that will work for versions prior to ios6, for those interested:

向后兼容的解决方案适用于 ios6 之前的版本,对于那些感兴趣的人:

- (void)unwindToViewControllerOfClass:(Class)vcClass animated:(BOOL)animated {

    for (int i=self.navigationController.viewControllers.count - 1; i >= 0; i--) {
        UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:i];
        if ([vc isKindOfClass:vcClass]) {
            [self.navigationController popToViewController:vc animated:animated];
            return;
        }
    }
}

回答by wm.p1us

SWIFT 4:

快速 4

1.Create an @IBActionwith segue inside controller you want to unwind to:

1.创建一个@IBAction,在控制器中使用 segue 来解压

    @IBAction func unwindToVC(segue: UIStoryboardSegue) {

    }

2.In the storyboard, from the controller you want to segue (unwind) from ctrl+drag from the controller sign to exit sign and choose method you created earlier:

2.在情节提要中,从您要从控制器上按 ctrl+drag 从控制器标志转到退出标志并选择您之前创建的方法:

enter image description here

在此处输入图片说明

3.Now you can notice that in document outline you have new line with title "Unwind segue....". Now you should click on this line and open attribute inspector to set identifier (in my case unwindSegueIdentifier).

3.现在你可以注意到在文档大纲中你有一个标题为“Unwind segue....”的新行。现在你应该点击这一行并打开属性检查器来设置标识符(在我的例子中是unwindSegueIdentifier)。

enter image description here

在此处输入图片说明

4.You're almost done! Now you need to open view controller you wish to unwind from and create some method that will perform segue. For example you can add button, connect it with code with @IBAction, after that inside this IBActionadd perfromSegue(withIdentifier:sender:)method:

4.你快完成了!现在你需要打开你想要放松的视图控制器并创建一些将执行转场的方法。例如,您可以添加按钮,使用@IBAction将其与代码连接,然后在此IBAction 中添加perfromSegue(withIdentifier:sender:)方法:

     @IBAction func unwindToSomeVCWithSegue(_ sender: UIButton) {
         performSegue(withIdentifier: "unwindSegueIdentifier", sender: nil)
     }

So that is all you have to do!

所以这就是你所要做的!

回答by 10623169

Swift 4.2, Xcode 10+

斯威夫特 4.2,Xcode 10+

For those wondering how to do this with VCs not set up via the storyboard(those coming to this question from searching "programmatically" + "unwind segue").

对于那些想知道如何使用不是通过故事板设置的VC 来做到这一点的人(那些通过搜索“以编程方式”+“展开转场”来解决这个问题的人)。

Given that you cannot set up an unwind segue programatically, the simplest solely programmatic solution is to call:

鉴于您无法以编程方式设置 unwind segue,最简单的单独编程解决方案是调用:

navigationController?.popToRootViewController(animated: true)

navigationController?.popToRootViewController(animated: true)

which will pop all view controllers on the stack back to your root view controller.

这会将堆栈上的所有视图控制器弹出回您的根视图控制器。



To pop just the topmost view controller from the navigation stack, use:

要从导航堆栈中弹出最顶层的视图控制器,请使用:

navigationController?.popViewController(animated: true)

navigationController?.popViewController(animated: true)

回答by John Verco

FYI: In order for @Vadim's answer to work with a manual unwind seque action called from within a View Controller you must place the command:

仅供参考:为了让@Vadim 的答案与从视图控制器中调用的手动展开序列操作一起使用,您必须放置以下命令:

[self performSegueWithIdentifier:(NSString*) identifier sender:(id) sender];

inside of the overriden class method viewDidAppearlike so:

在重写类方法viewDidAppear内部,如下所示:

-(void) viewDidAppear:(BOOL) animated
{
    [super viewDidAppear: animated];

    [self performSegueWithIdentifier:@"SomeSegueIdentifier" sender:self];
}

If you put it in other ViewController methods like viewDidLoador viewWillAppearit will be ignored.

如果将它放在其他 ViewController 方法中,例如viewDidLoadviewWillAppear,它将被忽略。