1

I'm creating a barcode reader for my app. I nailed that, yet it's useless if it doesn't to anything with the barcode it reads. What I want to do next is initiate a new View Controller as soon as the function captures the code.

Question is: How can I generate a new view controller as soon as this IF is satisfied?

if metadataObj.stringValue != nil 
{
    messageLabel.text = metadataObj.stringValue
}

This is the if that prints (in front of the user) the numeric form of the barcode (so when the user points his camera at a barcode, 4058394759 is displayed). Next, I want the app to move to a new view controller where that numeric value is printed and create a button that will generate a google search.

Thing is, I've only connected view controllers using the Interface Builder. How can I make this transition through code?

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
Daniel Ch
  • 11
  • 1
  • 1
    Possible duplicate of https://stackoverflow.com/questions/24167073/how-to-transition-to-a-new-view-controller-with-code-only-using-swift – Code Different Jul 16 '17 at 13:44
  • 1
    Possible duplicate of [IOS - How to segue programmatically using swift](https://stackoverflow.com/questions/27604192/ios-how-to-segue-programmatically-using-swift) – Lou Franco Jul 16 '17 at 14:14

1 Answers1

0

You should check the links provided in the comments so that you understand how this works. There are many ways to do this, and I will show just one (with and without a special initializer):

if metadataObj.stringValue != nil 
{
    messageLabel.text = metadataObj.stringValue
    let vc = yourViewController(initValue: metadataObj.stringValue!)
    self.navigationController?.push(vc, animated: true)
}

You could also present, rather than push your viewController. You can also do this without using a navigationController. Each of these options has consequences, which is why you are being urged to check the links so you understand your options.

I assumed your view controller has a specially written init to initialize with your required data. If that's not the case, then just assign the value to a property you've defined in your destination view controller before presenting/pushing it:

if metadataObj.stringValue != nil 
{
    messageLabel.text = metadataObj.stringValue
    let vc = yourViewController()
    vc.somePropertyYouDefined = metadataObj.stringValue!
    self.navigationController?.push(vc, animated: true)
}
Mozahler
  • 4,958
  • 6
  • 36
  • 56