0

I am trying to read a byte array from my one view controller to another, please find my code below.

From my First View

class First: UIViewController {

var myByteArray = [UInt8](repeating:0, count: 20)

viewDidLoad(){
......}

Few statements later hers where I read my data in a function

func passThis(){

let ReceiveData = rxCharacteristic?.value
        if let ReceiveData = ReceiveData {
            let ReceivedNoOfBytes = ReceiveData.count
            myByteArray = [UInt8](repeating: 0, count: ReceivedNoOfBytes)
            (ReceiveData as NSData).getBytes(&myByteArray, length: ReceivedNoOfBytes)
            print("Data Received ",myByteArray)
               }

This is my Second View that I'm trying to read my array from First View

class Second: UIViewController {

var myByteArray2 = [UInt8](repeating: 0, count: 20)

viewDidLoad(){
super.viewDidLoad()
let fvc = First()
myByteArray2 = fvc.myByteArray

print(myByteArray2)
}

Now I have [11,12,13,14,15,16,17,18,19,20] from myByteArray but have [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] from myByteArray2 ?

Can somebody help?

Also how do I clear the readValue buffer in rxCharacterstic before writing and reading new values?

Any help/comments appreciated.

Thanks

EDIT -> How my passing is done

From BLECentral

class BLECentral: ...

var centralManager: CBCentralManager!

//After Scanning and connecting

func centralManager(_central: CBCentralManager, didConnect peripheral: CBPeripheral){

peripheral.delegate = self

peripheral.discoverServices([BLEUUID])



//Once connected, move to new view controller to manager incoming and outgoing data
        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        let firstVC = storyboard.instantiateViewController(withIdentifier: "First") as! First

        firstVC.peripheral = peripheral

        navigationController?.pushViewController(firstVC, animated: true)
    }

Now in my First under prepare for segue block I'm passing the peripheral like this

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 if segue.destination is Second
{
let vc2 = segue.destination as? Second
vc2.periperal = blePeripheral
}
}
nar0909
  • 135
  • 2
  • 14
  • How are you transitioning view controllers? Is First calling Second? – Diesel Dec 18 '17 at 01:39
  • @Diesel Hi Diesel, Many thanks I was able to solve it with a little reading from your answer :) – nar0909 Dec 18 '17 at 04:09
  • No problem. What you are doing is correct! I recommend going through the Apple tutorial (just read it) as it will save you hours and cover the basics. https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ is a good place to start or their free books if you have more time on the Book Store called "App Development with Swift." – Diesel Dec 18 '17 at 20:04

1 Answers1

0

You are creating a new First view controller from your Second view controller, instead of accessing the one already created.

//Create a new View Controller
let fvc = First()

So you have two First that are made now. What you want to do is access the already created First view controller. Assuming there is no other way, you want to have a "Singleton". This is a very bad way to handle this, as I'll explain later, and there is most likely a better way, but I'm going to give a solution to access First from Second if they never communicate, but First is already created.

If you declare in First a piece of code like:

static let shared = First()

Then that singleton can be accessed via

let first = First.shared

That being said, that's a really bad way of handling communication between view controllers. If you call Second from First, you should pass the data from First to Second (or you could pass a reference of First to Second so Second can access First).

There is generally good ways to pass data between view controllers in the

func prepare(for segue: UIStoryboardSegue, sender: Any?) 

method before you navigate. Whenever, whatever, makes the Second view controller should pass it the data it needs.

Lastly, another reason the Singleton view controller is a terrible idea is that it gets away from the MVC concept. So if you can't pass the proper data to Second, you probably need a new class that handles the data for you that both First and Second can work with which is the Model in MVC.

Diesel
  • 5,099
  • 7
  • 43
  • 81
  • Hi Thanks for your response, https://stackoverflow.com/questions/47824265/how-to-modify-an-existing-blecentral-class-into-a-singleton-class This is exactly what I have , so can you help me now on how it needs to be done? I don't want it as singleton, I can read and write in one view controller and I just want the same functionality passed to my other views? Any help appreciated. – nar0909 Dec 18 '17 at 02:03
  • Please check my edit on how I pass the peripheral. Many Thanks – nar0909 Dec 18 '17 at 02:16
  • I'm glad you solved the problem. Realize, if you have lots of data, you may create a whole new class that handles that data. For example, I have an app with multiple food places in it. My class goes out and downloads places, stores them, and has functions like getPlace or getAllPlaces or refreshPlaces. It is a singleton as well, so everyone has access to the places. That way I don't have to download the data each time I need it. Here is a pretty good read on MVC: https://www.raywenderlich.com/132662/mvc-in-ios-a-modern-approach – Diesel Dec 18 '17 at 20:14