biggreentree
biggreentree

Reputation: 1943

swift how to update first controller from logic in second one vai protocol and delegate pattern?

I have a label in first view controller ViewController, and a func getting date avery second in second vc. I'd like to update label in first after timer starts in second. is it good to use protocol-delegate pattern? at this moment it is not working, time is going but not updating the view in first VC

my struct for protocol

protocol ViewControllerDelegate: class {        
    func changeLabelText(textToPass: String)        
}

in first viewController

class ViewController: UIViewController, ViewControllerDelegate {


    @IBOutlet weak var mainLabel: UILabel!        

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


    func changeLabelText(textToPass: String) {
        self.mainLabel.text = textToPass
        self.view.layoutIfNeeded()
    }

    @IBAction func buttonTapped(_ sender: UIButton) {            

        let nextVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
        nextVC.delegateSubscriber = self            
        present(nextVC, animated: true, completion: nil)            
    }

}

in secondVC

class SecondViewController: UIViewController {

    //MARK: speed timer feature 1/3
    private weak var timer: Timer?
    private var timerDispatchSourceTimer : DispatchSourceTimer?

    weak var delegateSubscriber : ViewControllerDelegate?       

    @IBOutlet weak var myTxtField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()            
        startTimer(every: 1)            
    }

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

        print("appeared")
        stopTimer()

    }

    private func startTimer(every timeInterval: TimeInterval) {
        if #available(iOS 10.0, *) {
            timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: true) { [weak self] _ in

                let dateToPass = Date().description
                print(dateToPass)
                self?.delegateSubscriber?.changeLabelText(textToPass: dateToPass)

            }
        }
    }

    //MARK: speed timer feature 3/3
    private func stopTimer() {
        timer?.invalidate()
        //timerDispatchSourceTimer?.suspend() // if you want to suspend timer
        timerDispatchSourceTimer?.cancel()
    }

    @IBAction func buttonTapped(_ sender: UIButton) {

//        delegateSubscriber?.changeLabelText(textToPass: self.myTxtField.text ?? "error")

        dismiss(animated: true, completion: nil)
    }        
}

Upvotes: 0

Views: 91

Answers (1)

Robert Dresler
Robert Dresler

Reputation: 11210

Just remove [weak self] from Timer closure

timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
    let dateToPass = Date().description
    print(dateToPass)
    self.delegateSubscriber?.changeLabelText(textToPass: dateToPass)
}

... then self isn't optional

Upvotes: 0

Related Questions