如何更好地发送完整的闭包

[英]How to send a complete closure better


What I want is to send a closures to a UIViewController to tell it what it will do in the end. But when it comes to a package of UIViewControllers it will be a little messy.

我想要的是向UIViewController发送一个闭包,告诉它最终会做什么。但是当谈到一个UIViewControllers包时,它会有点混乱。

class ViewController: UIViewController {

    private var complete: ()->()

    init(complete: ()->()){
        self.complete = complete
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

class ViewController2: UIViewController {

    private var complete: ()->()

    init(complete: ()->()){
        self.complete = complete
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

Here are my UIViewControllers. What want to do is to send a complete closure to UIViewController2, but I have to push UIViewController first. So what I have to do is send the closure to UIViewController, then UIViewController send the closure to UIViewController2. It is not messy when there are only two UIViewControllers. But it will be very messy when there comes out a package of UIViewControllers.

这是我的UIViewControllers。想要做的是向UIViewController2发送一个完整的闭包,但我必须首先推送UIViewController。所以我要做的是将闭包发送到UIViewController,然后UIViewController将闭包发送到UIViewController2。当只有两个UIViewControllers时,它并不凌乱。但是当出现一个UIViewControllers包时会非常混乱。

Any better solutions?

更好的解决方案?

1 个解决方案

#1


1  

You the following code to handle the completion handler

您可以使用以下代码来处理完成处理程序

First, create a base class of UIViewController and define the completionHandler

首先,创建UIViewController的基类并定义completionHandler

import UIKit

public class MyController: UIViewController {

var completionHandler: ((Bool, String) -> Void)? // Bool and String are the return type, you can choose anything you want to return

public func onComplete(completion : ((Bool, String) -> Void)?)
{
    self.completionHandler = completion
}

override public func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

override public func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

In ViewController1, you need to call another ViewController like this

在ViewController1中,您需要像这样调用另一个ViewController

class ViewController: MyController {

// Initialization of view objects
override func viewDidLoad()
{
    super.viewDidLoad()

    //You are loading another ViewController from current ViewController
    let controller2 = self.storyboard?.instantiateViewControllerWithIdentifier("controller2") as? MyController
    controller2?.onComplete({ (finished, event) in
        self.completionHandler?(finished, event)
    })
}

//This is your button action or could be any event on which you will fire the completion handler

@IBAction func buttonTapped()
{
    self.completionHandler(boolType, controllerName)
}

and where ever, you will create a new ViewController you will need to set its completionHandler by writing the line

无论如何,您将创建一个新的ViewController,您需要通过编写该行来设置其completionHandler

controller2?.onComplete({ (finished, event) in
    self.completionHandler?(finished, event)
}) 
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2016/06/27/7203013bdcd91115414ac11414310bd3.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告