In general, I rewrote with obj c the controller will not go into details, the point is, I have never when faced with the programming of interfaces and in this I am a complete noob,
In general, I ask you forum users to help me scatter logic, here is a common code
import UIKit import CoreBluetooth import PrinterLibs class BLE: UIViewController, BLEPrintingOpenDelegate, BLEPrintingDisconnectDelegate, BLEPrintingDiscoverDelegate, NETPrintingOpenDelegate, NETPrintingDisconnectDelegate, UITextFieldDelegate { var myBle = BLEPrinting() var myNet = NETPrinting() var myPos = POSPrinting() var myLabel = LabelPrinting() var myQueue = DispatchQueue(label: "BLEPrinting IO Queue") var rssi: NSNumber? var peripheral: CBPeripheral? var arrayPeripehral = [CBPeripheral]() var arrayPeripheralName = [String]() @IBOutlet weak var scrollView: UIScrollView! //var scrollView: UIScrollView? var tfRec: UITextField? @IBOutlet weak var search: UIButton! override func viewDidLoad() { super.viewDidLoad() myBle?.myDiscoverDelegate = self myBle?.myDisconnectDelegate = self myBle?.myOpenDelegate = self let width: Int = Int(view.bounds.size.width) //let height: Int = Int(view.bounds.size.height) let tf1 = UITextField(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(width), height: CGFloat(20))) tf1.textAlignment = .center tf1.isEnabled = false view.addSubview(tf1) let buttonDisconnect = UIButton(type: .roundedRect) buttonDisconnect.setTitle("disconect✖️", for: .normal) buttonDisconnect.frame = CGRect(x: CGFloat(width - 100), y: CGFloat(20), width: CGFloat(100), height: CGFloat(60)) buttonDisconnect.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin] buttonDisconnect.contentHorizontalAlignment = .right buttonDisconnect.addTarget(self, action: #selector(self.handleDisconnect), for: .touchUpInside) view.addSubview(buttonDisconnect) tfRec = UITextField(frame: CGRect(x: CGFloat(0), y: CGFloat(20), width: CGFloat(width), height: CGFloat(60))) tfRec?.textAlignment = .center tfRec?.contentVerticalAlignment = .center tfRec?.contentHorizontalAlignment = .center tfRec?.text = "" tfRec?.isEnabled = false view.addSubview(tfRec!) } @IBAction func searchBtn(_ sender: Any) { arrayPeripehral.removeAll() arrayPeripheralName.removeAll() for subview: UIView in (scrollView?.subviews)! { if (subview is UIButton) { subview.removeFromSuperview() } } myBle?.scan() } func handleConnect(_ sender: Any) { let btn = sender as! UIButton if (myBle?.isOpened())! { tfRec?.text = "Please disconnect first ...\r\n" return } myBle?.stopScan() let index: Int? = (arrayPeripheralName as NSArray).index(of: btn.title(for: .normal) as Any) let peripheral: CBPeripheral? = arrayPeripehral[index!] tfRec?.text = "Connecting...\r\n" myQueue.async(execute: {() -> Void in print("\(peripheral as Any)") if (self.myBle?.open(peripheral))! { self.myPos?.setIO(self.myBle) self.myLabel?.setIO(self.myBle) DispatchQueue.main.async(execute: {() -> Void in self.tfRec?.text = "Connected\r\n" self.dismiss(animated: true, completion: {() -> Void in print("Open Success. Jump to print page.") }) }) let text = "text printng" let qr = "qr printing" self.myPos?.pos_PrintText(UnsafeMutablePointer<Int8>(mutating: text)!, x: 10, nWidthTimes: 0, nHeightTimes: 0, nFontType: 0, nFontStyle: 1) self.myPos?.pos_PrintQRcode(UnsafeMutablePointer<Int8>(mutating: qr)!, x: -2, nUnitWidth: 10, nVersion: 0, nECCLevel: 4) } else { DispatchQueue.main.async(execute: {() -> Void in self.tfRec?.text = "Failed\r\n" }) } }) } func handleDisconnect(_ sender: Any) { myBle?.close() tfRec?.text = "Disconnected\r\n" } func didDiscoverBLE(_ peripheral: CBPeripheral!, address: String!, rssi: Int32) { DispatchQueue.main.async(execute: {() -> Void in if self.arrayPeripehral.contains(where: { $0.name == peripheral.name }) { return } self.arrayPeripehral.append(peripheral) let title: String = "Print button" self.arrayPeripheralName.append(title) let width = Int(self.view.bounds.size.width) //int height = self.view.bounds.size.height; let button1 = UIButton() button1.frame = CGRect(x: CGFloat(30), y: CGFloat((self.arrayPeripheralName.count - 1) * 40), width: CGFloat(width - 60), height: CGFloat(40)) button1.backgroundColor = UIColor.black button1.setTitle(title, for: .normal) button1.contentHorizontalAlignment = .left button1.addTarget(self, action: #selector(self.handleConnect), for: .touchUpInside) self.scrollView?.contentSize = CGSize(width: CGFloat(width), height: CGFloat(self.arrayPeripheralName.count * 40)) self.scrollView?.addSubview(button1) }) } } The essence of what I have when running VC it ​​has a set of connect search buttons as well as a scrollView which comes with a list of devices that were detected by ble
I want to make it so that when finding the name of the device they come to the tableView
and when you clicked on the device, a pair was created and when the pair was successfully created, a print button appeared.
Now I’m following up when a device is detected, a button appears that is added to srcollView and when I click on it, I first create a pair with a password and then nothing happens, then I restart the app and when I click on search, it appears in my scrollView I click on the button and then the printer starts printing on this event, tell me if I get around this if I have explained something wrong, I will be happy to explain ...