Home > Blockchain >  Swift - Newbie question. Passing variable values between different files, functions and views
Swift - Newbie question. Passing variable values between different files, functions and views

Time:03-29

newbie swift programmer here that needs help with something that probably is trivial...

This is in different File, different View:

class CanvasContainerView: UIView {
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
   
    // here are all of the calculations that lead to scoreString being calculated..

        var scoreString = round(1000 * accuracyTotal / angleTotal)
        }
//lots of other code
}

I need the value of my calculated "scoreString" to be accessible in a different file, different controller. How can i pass this value through if i'm using TouchesMoved function ? Below is how i tried to implement it in different file but failed miserably as there is no "scoreString" in scope:

import UIKit

class CanvasMainViewController: UIViewController {

    @IBOutlet weak var ScoreText: UITextField!
 
    /// Prepare the drawing canvas.
    /// - Tag: CanvasMainViewController-viewDidLoad
    override func viewDidLoad() {
        
        super.viewDidLoad()
        ScoreText.text = "\(CanvasContainerView.touchesMoved(scoreString))"
}
//lots of code
}

In the end i want my score to be displayed on top of the screen: enter image description here

So i am aware that there are questions like this already on Stack but whats specific about my question is that im using this "touchesMoved" overriden function that doesn't let me return any value. It shouts it needs void return type.

I will be very thankful for any help.

CodePudding user response:

Using a delegate is the good method :

// the protocol (ie the method called by container view in the controller)
protocol CanvasContainerViewDelegate {
    func scoreUpdate(fromView view:CanvasContainerView, newScore: Double)
}

class CanvasContainerView: UIView {
    // the delegate that will be called
    var delegate: CanvasContainerViewDelegate?
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
   
    // here are all of the calculations that lead to scoreString being calculated..

        var scoreString = round(1000 * accuracyTotal / angleTotal)
        // Telling the delegate the new score value
        delegate?.scoreUpdate(fromView: self, newScore: scoreString)
        }
//lots of other code
}

class CanvasMainViewController: UIViewController, CanvasContainerViewDelegate {
    
    @IBOutlet weak var ScoreText: UITextField!
    @IBOutlet weak var containerView: CanvasContainerView!
    
    /// Prepare the drawing canvas.
    /// - Tag: CanvasMainViewController-viewDidLoad
    override func viewDidLoad() {
        
        super.viewDidLoad()
        // set the controller as a delegate of the view
        containerView.delegate = self
    }
    
    // Update the score (called by the view)
    func scoreUpdate(fromView view: CanvasContainerView, newScore: Double) {
        ScoreText.text = "\(newScore)"
    }
    
    
    //lots of code
}

CodePudding user response:

Apparently adding public in front of the var declared on top of the file solved the problem. Thank you for participation in trying to help me.

  • Related