我有下面列出的2个控制器。我正在使用委托来尝试创建一个progressWindow,它将运行代码并很好地打印它,但是代码是任意的。
闭包是由符合协议的类(在我的例子中是SyncViewController)定义的,但是我想将progressWindowViewController的UI从SyncViewControllers codeToRun{}闭包中更改。我该怎么做?
同步视图控制器.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.

        }
    }

}

进度窗口视图控制器.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.
    }
    */

}

一个可能使用his的例子是下载数千个带有图像的库存记录,当它将库存详细信息捕获到progressWindow中时,这些记录将打印出来。
但是这个progressWindow也可以用于其他需要将特定内容打印到progressWindow文本区域的大型/小型任务(比如登录,因此来自不同的视图控制器,而不是我的示例中的sync)。我们的想法是让它成为一个动态的类。

最佳答案

不是创建变量,而是使用函数/方法?

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

关于ios - swift如何关闭类的引用属性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36495208/

10-11 08:24