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
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)
})
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/06/27/7203013bdcd91115414ac11414310bd3.html。