0

I have marked the part of my code where the problem is, it is commented out. The error message is:

Cannot assign value of type String! to type UILabel!.

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "SendDataSegue" {
        if let sendToDetailViewController = segue.destinationViewController as? DetailViewController {
            var sendingText = metadataObj.stringValue
            sendToDetailViewController.messageLabelDos = sendingText
        }
    }
}

The label it should be changing is in my DetailViewController and it is a label. The code above is from my original ViewController. How can I make this work?

More code to put in context:

if metadataObj.stringValue != nil {

dispatch_async(dispatch_get_main_queue()) {
    self.performSegueWithIdentifier("SendDataSegue", sender: self)
}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "SendDataSegue" {
        if let sendToDetailViewController = segue.destinationViewController as? DetailViewController {
            var sendingText = metadataObj.stringValue
            sendToDetailViewController.viaSegue = sendingText
        }
    }
}
Bista
  • 7,869
  • 3
  • 27
  • 55
Mus Harlem
  • 185
  • 4
  • 19

1 Answers1

3

You need to pass the String instead of setting text to label, because when you correct it and set like this sendToDetailViewController.messageLabelDos.text = sendingText, you will get nil error because messageLabelDos is not initialize yet, so try like this. Create one string instance in DetailViewController and use that inside prepareForSegue for passing String and then use that String instance in viewDidLoad to assign Label to text.

class ViewController: UIViewController { 

    //Your other methods

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
        if segue.identifier == "SendDataSegue" {
            if let sendToDetailViewController = segue.destinationViewController as? DetailViewController {
                var sendingText = metadataObj.stringValue
                sendToDetailViewController.messageDos = sendingText
            }
        }
    }
}

Inside DetailViewController

var messageDos: String = ""

override func viewDidLoad() {
    super.viewDidLoad()
    self.messageLabelDos.text = messageDos
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • that sure made the error go away, but I am unfortunately not changing the label on the DetailViewController? See more of my code above, as I have updated your post. – Mus Harlem Aug 31 '16 at 15:44
  • this is my instance `var viaSegue = ""` and this is inside the viewDidLoad: `self.messageLabelDos.text = viaSegue` – Mus Harlem Aug 31 '16 at 15:49
  • Should I put all of my code in my question, so you can see? – Mus Harlem Aug 31 '16 at 15:56
  • I have tried putting it outside of every other method, but without luck :( – Mus Harlem Aug 31 '16 at 16:02
  • This part is being executed `dispatch_async(dispatch_get_main_queue()){ self.performSegueWithIdentifier("SendDataSegue", sender: self) }` But it just does not chance the label on the other viewcontroller, it is just blank. – Mus Harlem Aug 31 '16 at 16:20
  • I have now written the prepareForSegue function where you wanted me to, but then it gives me this error: Use of unresolved identifier 'metadataObj' :( – Mus Harlem Aug 31 '16 at 19:48
  • Ok i will solve that error but you need to tell me the type of `metadataObj`, means which type of object it is, if it is custom class tell me the name of class. – Nirav D Sep 01 '16 at 04:22