Search code examples
swiftsprite-kituikitscene

Use of unresolved identifier 'scene'


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)
        }

Solution

  • 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.