2

I am attempting add barcode scanning to my app using RSBarcodes. I'm having two issues: Not being able to update a label that displays the barcode scanned and the delegate to send the barcode to my calling view controller not working. Below is my code for the view controller that handles the scanning:

import UIKit
import AVFoundation
import RSBarcodes

class ScanViewController: RSCodeReaderViewController {
@IBOutlet weak var label1Label: UILabel!
@IBOutlet weak var label2Label: UILabel!
@IBOutlet weak var scanLabel: UIButton!

var delegate: barcodesScannedDelegate?
var codes:[String] = []
override func viewDidLoad() {
    super.viewDidLoad()

    var code=""
    // Do any additional setup after loading the view.
    focusMarkLayer.strokeColor = UIColor.redColor().CGColor

    cornersLayer.strokeColor = UIColor.yellowColor().CGColor

    tapHandler = { point in
        //println(point)
    }

    barcodesHandler = { barcodes in

        for barcode in barcodes {
            if !contains(self.codes, barcode.stringValue) {
                self.codes.append(barcode.stringValue)
                code = barcode.stringValue
            }

        }
        println(code)
        self.label1Label.text = code
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func finishedPressed(sender: UIButton) {
    delegate?.barcodesScanned(self.codes)
    self.dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func cancelPressed(sender: UIButton) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

}

Just to ensure I did the delegate correctly, here is my code in Protocol.swift:

protocol selectCarrierDelegate {
   func selectCarrier(carrierID: String,carrier: String)
}

protocol barcodesScannedDelegate {
    func barcodesScanned(barcodes: [String])
}

And the relevant code in the controller that should receive the barcode:

class InBoundViewController: UIViewController,selectCarrierDelegate,UIAlertViewDelegate,UITableViewDelegate,UITableViewDataSource,barcodesScannedDelegate {

func barcodesScanned(barcodes: [String]) {
    println("codes=\(barcodes)")
}

Anyone have any ideas why the label won't change and the delegate isn't working?

ahsteele
  • 26,243
  • 28
  • 134
  • 248
Floyd Resler
  • 1,786
  • 3
  • 22
  • 41
  • In InBoundViewController, did you set the ScanViewController object's delegate to self? – Ron Fessler Dec 03 '14 at 19:29
  • Of course not. That would have made too much sense! I knew I had to be missing something simple. I'm still not sure why the label isn't updating but at least the delegate works. Thanks! – Floyd Resler Dec 03 '14 at 20:14

1 Answers1

4

You need to update all UI in the main thread.

Try this:

dispatch_async(dispatch_get_main_queue(), { () -> Void in
    self.label1Label.text = code
})

Swift 4:

DispatchQueue.main.async {
    self.label1Label.text = code
}
Dinsen
  • 2,139
  • 3
  • 19
  • 25