-2

I have made a func in ViewController, that can receive a string and change the title of a label, that is located on screen.

Here it is:

func setNewTitleForMainLabelOnScreen(text: String){
    
    mainLabelOnScreen.cell?.title = text
    
}

What i want to do, is to call this function (viewController's method?) from another class.

Like that:

 class MainLabelEditor{

      func updateLabelOnScreen{

            ViewController.setNewTitleForMainLabelOnScreen(text: "Hello")

      }

}

Unfortunately, it doesn't work.

Here is what xcode says:

enter image description here

If I press return, it shows this:

enter image description here

The question is, how do i call setNewTitleForMainLabelOnScreen from MainLabelEditor?

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
boring_ape
  • 59
  • 3
  • Your `MainLabelEditor` instance needs a reference to the `ViewController` instance that is on screen. What is the relationship between these two classes? – Paulw11 Aug 07 '21 at 13:17
  • Be more precise when you sa it does not work : is it a build error or an excution error ? In your case it seems that you want to access the content of a cell in a view controller. In this case you should save the UILabel in the cell in the viewController. – Ptit Xav Aug 07 '21 at 13:23
  • What is a reference and how can i do it? Yes, it is a build error. I'll add a screenshot – boring_ape Aug 07 '21 at 13:28
  • What you want is the "delegation pattern". You create a protocol with the desired methods, you make one class conform to these methods (implement them) and then make the other class the delegate of the previous one. There's many tutos about that, and many QAs on this site. – Eric Aya Aug 07 '21 at 13:38
  • You're welcome. My explanation was a bit messed up (hard to describe this in comments and English is not my first language) but using a delegate is indeed a solution that will work for you in this case. – Eric Aya Aug 07 '21 at 13:48
  • By the way, Matt was right: the reason I didn't post an answer is because there's already a lot of QAs covering this on the site. My comment was just to point you in the right direction. You can either delete this question now, or if you find a solution in a QA you can mark your question as a "duplicate" of the solution's page. Good luck. :) – Eric Aya Aug 07 '21 at 14:21
  • Does this answer your question? [What is the difference between class and instance methods?](https://stackoverflow.com/questions/1053592/what-is-the-difference-between-class-and-instance-methods) – hasan Aug 15 '21 at 11:00

3 Answers3

0

I usually use NotificationCenter to deliver data from other ViewController.

ViewController

   override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(setNewTitleForMainLabelOnScreen), name: NSNotification.Name(rawValue: "changeTitle"), object: nil)
        
    }

   @objc func setNewTitleForMainLabelOnScreen(_ notification: Notification){
        
        mainLabelOnScreen.cell?.title = notification.object as! String
        
    }

: Specifies the value passed to NotificationCenter object as title.

Other UIViewController

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeTitle"), object: "titleTextValue")

: You can add the string value you want as the title to the object.

Hailey
  • 302
  • 1
  • 7
0

You can try:

public class func setNewTitleForMainLabelOnScreen(text: String){
     mainLabelOnScreen.cell?.title = text         
}
Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
J.Doe
  • 86
  • 1
  • 5
0

So it turns out, all you need to do is to type

ViewController().setNewTitleForMainLabelOnScreen(text: "Hello")

instead of

ViewController.setNewTitleForMainLabelOnScreen(text: "Hello")
boring_ape
  • 59
  • 3