如何在标签栏控制器之间传递数组变量?

时间:2019-04-27 10:59:27

标签: swift xcode uitabbarcontroller

我试图了解视图控制器的生命周期。我需要使用TabBar来切换控制器。而且,在切换控制器时,我需要标签来显示生命周期方法,不仅包括我现在发现的控制器的生命周期方法,还包括其他生命周期方法。

我创建了一个空数组private var arrayOfMethods = [String](),每次都在其中添加一个触发方法。

class ViewController: UIViewController {

    private var arrayOfMethods = [String]()

    @IBOutlet var greenLabel: UILabel!
    @IBOutlet var blueLabel: UILabel!
    @IBOutlet var yellowLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        printMessage()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        printMessage()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        printMessage()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        printMessage()
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        printMessage()
    }

    func printMessage(line: Int = #line, function: String = #function) {
        print("\(title ?? "nil"). Line: \(line) of function \(function)")
        arrayOfMethods.append(function)
        let string = arrayOfMethods.joined(separator: "\n")
        greenLabel.text = "\(title ?? "nil") \(string)"
    }
}

1 个答案:

答案 0 :(得分:1)

您可以创建一个通用类,并从所有视图控制器中调用其功能

 class Helper: NSObject {

    private var arrayOfMethods = [String]()
    static let shared = Helper()
    let mainLabel = UITextView(frame: CGRect(x: 100, y: 100, width: 300, height: 300))

    private override init() {
        super.init()
    }

    func printMessage(vc: UIViewController, line: Int = #line, function: String = #function) {
        print("\(vc.title ?? "nil"). Line: \(line) of function \(function)")
        arrayOfMethods.append( (vc.title ?? "nil") + "-" + function)
        let string = arrayOfMethods.joined(separator: "\n")

        guard let window = UIApplication.shared.keyWindow else { return }

        if !window.subviews.contains(mainLabel) {
            window.addSubview(mainLabel)
            window.bringSubviewToFront(mainLabel)
        }
        mainLabel.text = string
    }
}

并从所有这样的视图控制器中调用此单例类方法

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        Helper.shared.printMessage(vc: self)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        Helper.shared.printMessage(vc: self)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        Helper.shared.printMessage(vc: self)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        Helper.shared.printMessage(vc: self)
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        Helper.shared.printMessage(vc: self)
    }
}

输出

enter image description here