0

I am trying to programmatically change a scene when a 'barcode' has been found, but I keep getting errors...

Imported Modules:

import UIKit
import AVFoundation
import SpriteKit
import SceneKit

Code:

        if metadataObj.stringValue != nil {
            messageLabel.text = "Found: " + metadataObj.stringValue
            myBarcode = metadataObj.stringValue

            let transition = SKTransition.reveal(with: .down, duration: 1.0)

            let nextScene = WebLogger(size: scene!.size) *<-- use of unresolved identifier 'scene'
            nextScene.scaleMode = .AspectFill

            scene?.view?.presentScene(nextScene, transition: transition) *<-- use of unresolved identifier 'scene'
        }

Fixed issue 1 by adding

var scene = SKScene()

Issue 2 Incorrect argument label in call (have 'size:', expected 'coder:')

var scene = SKScene()

        if metadataObj.stringValue != nil {
            messageLabel.text = "Found: " + metadataObj.stringValue
            myBarcode = metadataObj.stringValue

            let transition = SKTransition.reveal(with: .down, duration: 1.0)

            let nextScene = WebLogger(size: scene!.size) *<-- Incorrect argument label in call (have 'size:', expected 'coder:')
            nextScene.scaleMode = .AspectFill

            scene?.view?.presentScene(nextScene, transition: transition)
        }
  • OK, where is `scene` defined? Could you show us some more code maybe, just to help us with the context. The code you've shown us here for instance, is that from a `ViewController` of sorts or in the `Scene` itself? – pbodsk Jun 02 '17 at 10:57
  • @pbodsk that is from a view controller. Here is the full script for it: https://pastebin.com/gzaXhJF9 and here is the view: http://imgur.com/a/gAM0b – Michael Davison Jun 02 '17 at 11:03
  • Would you mind posting it into your question...just so we have it in one place? – pbodsk Jun 02 '17 at 11:08
  • In the code you've linked to, the only places I can see a reference to `scene` are the places where the compiler complains. So that is why it complains at least...it doesn't know what `scene` is, because you haven't declared it before :) – pbodsk Jun 02 '17 at 11:09
  • Ok, I declared it using 'var scene = SKScene()' but now it says 'Incorrect argument laben in call (have 'size:', expected 'coder:') – Michael Davison Jun 02 '17 at 11:32
  • OK, I think we're misunderstanding each other here. Now...you have a `UIViewController` in which you would like to capture a barcode. Once that barcode has been captured you would like to navigate to...somewhere? At the moment you are trying to navigate to a `SKScene`...but you haven't initialized the whole `SKScene` setup. Try for instance to create a new SpriteKit game from the template in Xcode and notice how they instantiate the `SKScene` in the `GameViewController`. Another (annoying) question. Why are you interested in SpriteKit at all? Couldn't you just use a normal `ViewController`? – pbodsk Jun 02 '17 at 11:40
  • I have very minimal skills in XCode... After googling how to switch scenes programmatically in xcode, a few people give me a script for a 'GameView' scene switcher. If you have any easier code that i can plop into there, please do and i will mark it as an answer – Michael Davison Jun 02 '17 at 11:43
  • OK...no worries, we'll solve this :) So...just so I understand. Once the barcode has been captured you'd like to go to your `WebLogger` right? How is `WebLogger` defined? Is it a `UIViewController` for instance? – pbodsk Jun 02 '17 at 11:46
  • Yes, I would like it to open the WebLogger. It is a Controller Scene on the Main.storyboard file. http://imgur.com/a/gAM0b (Storyboard Layout) – Michael Davison Jun 02 '17 at 13:23
  • Yes, it is a UIViewController `Class WebLogger: UIViewController {` – Michael Davison Jun 02 '17 at 13:32

1 Answers1

0

OK. I think you may have to backtrack a bit from your current setup.

The keywords/key technologies you should search for tutorials about are:

  • storyboard: This is where you define/design the flow of your app, you seem to have that already.
  • UIViewController: In your storyboard you define the various viewcontrollers, which you the wire to represent the UIViewControllers in your app.
  • segue: a segue is how you navigate between UIViewControllers in a storyboard. You can wire the segues so they happen automatically when you tap a button for instance, or you can call the segues manually from your code (that is probably what you want in your case).

A Simple Example.

I've created a project with two UIViewControllers (FirstViewController and SecondViewController).

Project Structure

Between them I've added a UIStoryboardSegue as you can see. To do so you hold down ctrl and then you drag from the yellow box above the first UIViewController (shown on the image below) to anywhere on the second UIViewController and select your presentation style.

Drag from this bad boy

Remember to give your segue an identifier, we'll need that in a second. To do so, select the segue and look at the right side of the screen.

Identifier is important

The final step is to invoke the segue. I've added an IBAction to a button in my FirstViewController which looks like this:

@IBAction func navigateToPageTwo(_ sender: UIButton) {
   performSegue(withIdentifier: "mySegue", sender: nil)
}

(see...I told you the identifier was important ;)).

If you do that, then you should be able to navigate from one UIViewController to another.

In your case you should add a segue between your QRScannerController and your WebLogger and then do something along the lines of:

if metadataObj.stringValue != nil {
    messageLabel.text = "Found: " + metadataObj.stringValue
    myBarcode = metadataObj.stringValue

    performSegue(withIdentifier: "nameOfYourSegue", sender: nil)
}

If you need to pass data between the two UIViewControllers you should look into the method prepare(for segue: UIStoryboardSegue, sender: Any?) (have a look at this question and answer for instance)

Hope that gives you something to work with.

Community
  • 1
  • 1
pbodsk
  • 6,787
  • 3
  • 21
  • 51
  • Ok, I added the code and named the Storyboards.. But now the barcode scanner doesnt find anything so i cant really test it :/ EDIT: If I uncomment the performSegue the scanner works but then it wont do anything :( – Michael Davison Jun 02 '17 at 14:21
  • OK. And have you added a segue and given it a name that matches with the one you use when calling it? – pbodsk Jun 02 '17 at 15:13
  • Never mind :D I went through your answer on a new scene and got it working then understood how to put it into the other one! Working exactly how I wanted it! Thank you!! – Michael Davison Jun 02 '17 at 15:14
  • @MichaelDavison great! I admit that it can be complicated when you start out doing navigation, segues, storyboards and all that but you'll soon get the hang of it when you know what to search and ask for :) good luck onwards – pbodsk Jun 02 '17 at 15:26