它如何才能迅速closures它的类的引用属性运行?
我有以下2个控制器列出如下。 我正在使用委托来尝试创build一个progressWindow,它将运行代码并很好地打印它,但代码是任意的。
闭包由符合协议的类定义(在我的情况下是SyncViewController), 但是我想从SyncViewControllers codeToRun {}闭包中更改progressWindowViewController的UI。 我该怎么做呢?
SyncViewController.swift
import UIKit class SyncViewController: UIViewController, progressWindowDelegate { var codeToRun = { //(self as! ProgressWindowViewController).theTextView.text = "changed the text" print("code to run") } var codeToCancel = {print("code to cancel")} var titleToGive = "Starting Sync..." override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func yesSyncButtonAction(sender: UIButton) { //Segue to the ProgressWindowViewController... } @IBAction func noSyncActionButton(sender: UIButton) { tabBarController?.selectedIndex = 1 //assume back to inventory section } // MARK: - Navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if(segue.identifier == "SyncToProgressSegue"){ let progressWindow = segue.destinationViewController as! ProgressWindowViewController progressWindow.controllerDelegate = self //sets the delegate so we have reference to this window still. } } }
ProgressWindowViewController.swift
import UIKit protocol progressWindowDelegate{ var titleToGive : String {get} var codeToRun : ()->() {get} var codeToCancel : ()->() {get} } class ProgressWindowViewController: UIViewController { @IBOutlet weak var theTextView: UITextView! @IBOutlet weak var theProgressBar: UIProgressView! @IBOutlet weak var navItemLabel: UINavigationItem! //Sets delegate var controllerDelegate:progressWindowDelegate! override func viewDidLoad() { super.viewDidLoad() navItemLabel.title! = controllerDelegate.titleToGive dispatch_async(dispatch_get_main_queue(),{ self.controllerDelegate.codeToRun() //Will run code accordingly. }) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func cancelNavItemButtonAction(sender: UIBarButtonItem) { dispatch_async(dispatch_get_main_queue(),{ self.controllerDelegate.codeToCancel() }) } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }
如何使用他的例子是下载成千上万的库存logging与图像,这将打印库存的细节,因为它把它们抓到progressWindow。
但是这个progressWindow也可以用于其他需要在progressWindow textarea中输出特定内容的大型/小型任务(例如login,因此来自不同的视图控制器,而不是我的示例中的同步)。 这个想法是使它成为一个dynamic的类。
而不是创build一个variables,只需使用一个函数/方法?
override func viewDidLoad() { super.viewDidLoad() dispatch_async(dispatch_get_main_queue(),{ self.controllerDelegate?.codeToRun(self) }) }
。
protocol progressWindowDelegate : class { var titleToGive : String {get} func codeToRun(progressWindowViewController:ProgressWindowViewController) var codeToCancel : ()->() {get} } class SyncViewController: UIViewController, progressWindowDelegate { func codeToRun(progressWindowViewController:ProgressWindowViewController) { print("code to run") }
也使委托弱和可选:
delegate weak var controllerDelegate:progressWindowDelegate? = nil