SwiftUI如何将变量从UIView传递给类

时间:2020-03-26 18:40:17

标签: class swiftui ibeacon

我正在编写具有iBeacon检测功能的应用程序。我从API提取数据并列出到视图A中,然后导航到包含数据的详细视图。在详细视图中,有一个信标检测类。问题是我无法将设备“ UUID”传递给该类。似乎数据仅传递给详细信息视图,而不传递给类。有什么办法让我传递给类的UUID并替换为func startDect()

查看A

import SwiftUI

struct ScheduleView: View {

    @EnvironmentObject var settings : LoginAuth
    @ObservedObject private var scheduleListVM = scheduleservices()

    var body: some View {
         ZStack {
         ForEach(self.scheduleListVM.schedulelist, id: \.SNO) { schedule in
             NavigationLink(destination: EnrollmentView(schedule:schedule)) {

 CourseID: "\(schedule.CourseName)", CourseName:"", Date:"\

(schedule.Date)", StartTime: "Tutor:  \(schedule.StartTime)", EndTime: "Year:   \

(schedule.EndTime)", LocID: "", Room: "", Floor: "", Region: ""
    }
}

带有该类的详细视图

import Combine
import CoreLocation
import SwiftUI

class BeaconDector: NSObject, ObservableObject, CLLocationManagerDelegate {

    var objectWillChange = ObservableObjectPublisher()
    var LocationManager : CLLocationManager?
    var LastDistance = CLProximity.unknown

    override init(){
        super.init()

        LocationManager = CLLocationManager()
        LocationManager?.delegate = self
        LocationManager?.requestWhenInUseAuthorization()
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus ){
        if status == .authorizedWhenInUse {
            if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self){
                if CLLocationManager.isRangingAvailable(){
                    startDect()
                }
            }
        }
    }

    func startDect(){
        let uuid = UUID(uuidString:"5A4BCFCE-174E-4BAC-A814-092E77F6B7E5")!
        let cons = CLBeaconIdentityConstraint( uuid: uuid, major: 123, minor: 456)
        let beaconRegion = CLBeaconRegion(beaconIdentityConstraint: cons, identifier: "Test")
        LocationManager?.startMonitoring(for: beaconRegion)
        LocationManager?.startRangingBeacons(satisfying: cons)
    }

    func locationManager(_ manager: CLLocationManager, didRange beacons: [CLBeacon], satisfying beaconConstraint: CLBeaconIdentityConstraint) {
        if let beacon = beacons.first{
            update(distance: beacon.proximity)
        } else {
            update(distance: .unknown)
        }
    }

    func update( distance : CLProximity) {
        LastDistance = distance
        objectWillChange.send()
    }
}


struct EnrollmentView: View {
    let schedule: schedule
    @ObservedObject var scanner = BeaconDector()

    var body: some View {
         Text(schedule.UUID)
    }
}

0 个答案:

没有答案