ios segue完成后如何执行一些代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18310396/
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-31 00:47:52  来源:igfitidea点击:

How to execute some code after a segue is done?

iosios6uiviewcontrollersegueuistoryboardsegue

提问by bogen

Is it possible in iOS 6 to know when a UIStoryboardSeguehas finished its transition? Like when i add a UIStoryboardSeguefrom UIButtonto push another UIViewControlleron the navigationcontroler, i want to to something right after the push-transition is finished.

在 iOS 6 中是否有可能知道 a 何时UIStoryboardSegue完成转换?就像当我在导航控制器上添加一个UIStoryboardSeguefrom UIButton以推送另一个UIViewController时,我想在推送转换完成后立即执行某些操作。

采纳答案by New

You can use the UINavigationControllerDelegateprotocol and then define:

您可以使用该UINavigationControllerDelegate协议,然后定义:

– navigationController:didShowViewController:animated:

回答by Levi

In case you don't want to use the viewDidAppear:method, you could create a custom segue. In the performmethod you would use an animation for the transition, and that can have a completion block. You can add the code there after the animation is complete.

如果您不想使用该viewDidAppear:方法,您可以创建自定义转场。在该perform方法中,您将使用动画进行过渡,并且可以有一个完成块。您可以在动画完成后在那里添加代码。

回答by clearlight

In Swift, from a UIViewControllersubclass you can get the UINavigationControllerinstance and set the delegate, in order to be informed about the completion of segues, as shown. Another logical place to track segues might be the AppDelegate.

在 Swift 中,UIViewController您可以从子类中获取UINavigationController实例并设置委托,以便了解 segue 的完成情况,如图所示。跟踪转场的另一个合乎逻辑的地方可能是AppDelegate.

Example of doing it from a view controller (VC for short):

从视图控制器(简称 VC)执行此操作的示例:

class MyViewControllerSubclass : UIViewController, UINavigationControllerDelegate {

    func viewDidLoad() {
        self.navigationController.delegate = self
    }

    func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
        println("Did show VC: \(viewController)")
    }
 }

But that only shows you when the segue to the VC is complete, as would viewWillAppear()or viewDidAppear()delegate methods in the VC being presented; however, they don't inform about when the target VC is un-presented. It will also only work if your View Controller is part of a Navigation Controller stack.

但这仅在 VC 的 segue 完成时向您显示,就像VC 中呈现的viewWillAppear()viewDidAppear()委托方法一样;但是,它们不会通知目标 VC 何时呈现。它也仅在您的视图控制器是导航控制器堆栈的一部分时才有效。

In the VC you're tracking, you could add the following to detect when the VC (and its memory) are deallocated, or override the viewWillDisappear()method.

在您正在跟踪的 VC 中,您可以添加以下内容来检测 VC(及其内存)何时被释放,或覆盖该viewWillDisappear()方法。

deinit {
    println(__FUNCTION__, "\(self)")
}

回答by ahmedalkaff

You can use - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

您可以使用 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

This method will be called right before a segue is performed in the source UIViewController. If you want to do some code in the destination UIViewController you can get the destination viewcontroller of segue.

在源 UIViewController 中执行 segue 之前,将立即调用此方法。如果你想在目标 UIViewController 中做一些代码,你可以得到 segue 的目标视图控制器。

You can also add this code in the viewdidAppear in the desintation viewController.

您也可以在目标viewController 的viewdidAppear 中添加此代码。

回答by Usman Ahmad

you can call a method of destination UIViewController in prepareForSegue method.

您可以在 prepareForSegue 方法中调用目标 UIViewController 的方法。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  NSLog(@"prepareForSegue: %@", segue.identifier);

  if ([segue.identifier isEqualToString:@"Happy"]) {
      [segue.destinationViewController setHappiness:100];
  } else if ([segue.identifier isEqualToString:@"Sad"]) {
      [segue.destinationViewController setHappiness:0];
  }
}

here setHappiness method is of destination Controller and here 100 is passing there. so you can write a method in destination controller and call it here

这里 setHappiness 方法是目标控制器,这里 100 正在那里传递。所以你可以在目标控制器中编写一个方法并在此处调用它