0

I'm currently having an issue trying to pass data from a QR Code scanner controller to a view controller. The reason for doing this is because I'm gonna have 30+ Qr codes during an event, and all these codes will segue to the same view controller but with different label/data.

View Controller

var myString:String = ""
@IBOutlet var label: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    label?.text = myString

}

QR Scanner Controller

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondController = segue.destination as! Sample3Controller
  if segue.identifier == "Segue1"
       {
           secondController.myString = "Welcome to Booth 1"
       }
  else if segue.identifier == "Segue2"
       {
           secondController.myString = "Welcome to Booth 2"
       }
     }
            if metadataObj.stringValue == "Booth 1" //if qr code text is "Booth1"
            {
                captureSession?.stopRunning()
                self.performSegue(withIdentifier: "Segue1", sender: self)
            }
            else if metadataObj.stringValue == "Booth 2" // if qr code text is "Booth2"
            {
                captureSession?.stopRunning()
                self.performSegue(withIdentifier: "Segue2", sender: self)
            }
halfer
  • 19,824
  • 17
  • 99
  • 186
Kinja
  • 449
  • 5
  • 22
  • And what is the problem? – Retterdesdialogs Dec 11 '17 at 11:01
  • It can't work. my string doesn't display at all – Kinja Dec 11 '17 at 11:02
  • I don't understand what you are trying to archive. Why you are using different segues? Is the @IBOutlet correct connected? – Retterdesdialogs Dec 11 '17 at 11:06
  • Why call `performSegue ` inside `prepare(for segue)`?? Remove that part, not sure how your code work also – Tj3n Dec 11 '17 at 11:21
  • @Retterdesdialogs because i thght with different segues it will display different labels. For example if u see the QR Scanner controller, if the Segue identifier is "Segue1", it will display "Welcome to Booth 1" and or if its "Segue2", it will display "Welcome to Booth 2". So on and so forth. – Kinja Dec 11 '17 at 11:31
  • @Tj3n but its not, the perform segue is not in the prepare(for segue) func – Kinja Dec 11 '17 at 11:33
  • Put a breakpoint in the line `secondController.myString = ...` to see if it get called or not – Tj3n Dec 11 '17 at 11:35
  • @Kinja, this is the wrong approach. You only need one segue for this and pass the data to the view controller. Also Tj3n is right, the prepare is called just before the segue is already fired. – Retterdesdialogs Dec 11 '17 at 11:36
  • @Tj3n u're right it doesn't :/, there's a thread 1:breakpoint at the if metadataObj.Stringvalue == "Booth 5" – Kinja Dec 11 '17 at 11:48
  • @Retterdesdialogs and what would be the correct approach to this? because i'm super new to Xcode and all these codes are from youtube video tutorials. – Kinja Dec 11 '17 at 11:49
  • @Kinja https://stackoverflow.com/a/31934786/442121 – Retterdesdialogs Dec 11 '17 at 11:57
  • @Retterdesdialogs but he's using it with a textfield and i'm not, would the logic still be the same? – Kinja Dec 11 '17 at 12:03
  • @Kinja yes, it is still the same. You want to know how to pass data between view controllers the correct way. – Retterdesdialogs Dec 11 '17 at 12:04
  • @Retterdesdialogs bro i tried it, it still doesn't work under my QR code scanner logic – Kinja Dec 12 '17 at 01:34
  • @Retterdesdialogs nevermind i managed to solved it using user defaults – Kinja Dec 12 '17 at 01:56

0 Answers0