0

I am completely new to Swift programming and tried to delegate a single String from one ViewController to another by clicking a send button. The problem is , that it does not work ...

I guess it would be easy for you to solve this and considering that it would be very helpful wether you explain me what I did wrong. :)

Thank you a lot

import UIKit


protocol protoTYdelegate {
    func didSendMessage(message: String)
}


class New: UIViewController {

    @IBOutlet weak var SendButton: UIButton!

    var tydelegate: protoTYdelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

    }





    @IBAction func SendButtonAction(_ sender: Any) {

        let nachricht = "It works fine."
        tydelegate?.didSendMessage(message: nachricht)

    }

}

import UIKit


class ThankYouPage: UIViewController {


    @IBOutlet weak var numbersView: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        let controller = New()
        controller.tydelegate = self

    }

}

extension ThankYouPage: protoTYdelegate{
    func didSendMessage(message: String) {
        numbersView.text = message
    }
steveSarsawa
  • 1,559
  • 2
  • 14
  • 31
naheliegend
  • 159
  • 2
  • 3
  • 9
  • `let controller = New()` is not same as the view controller which is visible on the screen. You need to explain how both view controllers are connected in your code. – adev May 25 '19 at 18:28
  • i implemented a segue if I am clicking on the send button. – naheliegend May 25 '19 at 18:36
  • Could you please explain what exactly does not work in your app? – Roman Podymov May 25 '19 at 19:47
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – HirdayGupta May 26 '19 at 00:47

2 Answers2

2

As far as I understand, this code block doesn't work but the problem is not in the code, it's actually way that you choose to send data. In iOS development, there are many ways to send data. In your case, you need to use prepareForSegue method to send data to new class, not necessary to use delegates.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "ThankYouPage") {
    let vc = segue.destination as! ThankYouPage
    vc.message = "Message that you want to send"
    }
}

And you need to implement your ThankYouPage as:

class ThankYouPage: UIViewController {

@IBOutlet weak var numbersView: UILabel!
var message = ""

override func viewDidLoad() {
    super.viewDidLoad()

    numbersView.text = message
}

}

In addition to that, you can use didSet method to print out the message to label instead of printing it directly in viewDidLoad method. Simply:

class ThankYouPage: UIViewController {

@IBOutlet weak var numbersView: UILabel!
var message: String?{
    didSet{
      numbersView.text = message
  }
} 

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

}

I hope this helps you.

0

@Eyup Göymen's answer is right.

I have another way, assuming that you are not using segue and you are pushing to next controller by manual-code.

So your ThankYouPage code should be like :

import UIKit

class ThankYouPage: UIViewController {

    @IBOutlet weak var numbersView: UILabel!

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

    @IBAction func someButtonAction(_ sender: Any) { // By clicking on some, you are opening that `New` controller
         let detailView = self.storyboard?.instantiateViewController(withIdentifier: "New") as! New
         detailView.tydelegate = self
         self.navigationController?.pushViewController(detailView, animated: true)
    }
}

extension ThankYouPage: protoTYdelegate {

     func didSendMessage(message: String) {
         numbersView.text = message
    }
}
VRAwesome
  • 4,721
  • 5
  • 27
  • 52