0

I am trying a segue to pass a string from a view controller to another view controller but it's not working.

Xcode 10, Swift 4, iOS 10.

viewcontroller.swift code:

import UIKit

class ViewController: UIViewController {
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "segue_vc_to_myvc" {
            let vc = segue.destination as! MyViewController
            vc.str = "hello"
        }
    }
}

Myviewcontroller.swift:

import UIKit

class MyViewController: UIViewController {
    @IBOutlet weak var label:UILabel!

    var str: String? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        //label.text = str
        if let str = str {
            print(str)
        }
    }

I want to show the string on screen, but it's nothing. I tried to use a label and it still has nothing.

If I use label.text = str, the error messages is:

self    _0190603.MyViewController   0x000000010130a490
UIKit.UIViewController  UIViewController    
label   UILabel?    nil none
str String? nil none

set the break point and error message

segue.identifier

the label have nothing...

Angeloli
  • 1
  • 1

2 Answers2

0

In your storyboard check the Segue's identifier between the two page. enter image description here

then make sure you trigger the right way to preform the segue.

something like this

@IBAction func NextPageAction(_ sender: Any) {
    performSegue(withIdentifier: "second_page", sender: nil)
}

add a break point at 'if segue.identifier == blablabla' and see what happening here

   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "second_page" {
            let page = segue.destination as! SecondeViewController
            page.txt = "hello world"
        }
    }
Jack Martin
  • 129
  • 10
0

check whether you have set the identifier correctly. It should work as it is!

The Pedestrian
  • 460
  • 2
  • 11