Ios 在另一个类(scene.swift)中引用我的视图控制器-所有变量都变为null?斯威夫特3

Ios 在另一个类(scene.swift)中引用我的视图控制器-所有变量都变为null?斯威夫特3,ios,swift,viewcontroller,scene,touchesbegan,Ios,Swift,Viewcontroller,Scene,Touchesbegan,我正在尝试从我的Scene.swift类中的视图控制器调用一个方法,我能够做到这一点。当我单击在视图控制器中设置的SKLabel节点时,在调用touchesBegind方法之后调用该方法 问题是,当我点击一个SKLabelNode时,控件传递到Scene.swift类,在touchesBegind方法中,我要调用的方法被调用,因此控件被传递回视图控制器,当我回到这里时,我的所有变量似乎都设置为nil,好像它是控制器的一个完全不同的实例 当我尝试在ArViewController中设置标签的文本属

我正在尝试从我的Scene.swift类中的视图控制器调用一个方法,我能够做到这一点。当我单击在视图控制器中设置的SKLabel节点时,在调用touchesBegind方法之后调用该方法

问题是,当我点击一个SKLabelNode时,控件传递到Scene.swift类,在touchesBegind方法中,我要调用的方法被调用,因此控件被传递回视图控制器,当我回到这里时,我的所有变量似乎都设置为nil,好像它是控制器的一个完全不同的实例

当我尝试在ArViewController中设置标签的文本属性时,checkIfValidTime方法中出现错误。-我用**突出显示了这些行

错误:

Fatal error: Unexpectedly found nil while unwrapping an Optional value
如何引用视图控制器的同一实例,以便在场景中声明时不会重置变量。Swift? 或者有没有一种方法可以在视图控制器中实现touchesbearth方法,这样就不必实例化ARViewController

我非常感谢在这件事上的任何帮助,因为我在这件事上已经被困了一段时间,而且我对iOS和swift应用程序设计还不熟悉

我已尝试将代码限制在解释此问题所需的范围内。 有问题尽管问。谢谢

ARViewController:

    public var receivedCallback : Bool = false

    class ARViewController: UIViewController, ARSKViewDelegate, URLSessionDelegate {
        // MARK: - IBOutlets
        @IBOutlet weak var sceneView: ARSKView!
        @IBOutlet weak var guideLabel: UILabel!
        @IBOutlet weak var testLbl: UILabel!

 var scene : Scene?
        static var dateToUse : Date?
        var aRLocalDate : Date?

        var button: SKSpriteNode?

        override func viewDidLoad() {
            super.viewDidLoad()
        }

        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            /*
             Start the view's AR session with a configuration that uses the rear camera,
             device position and orientation tracking, and plane detection..
             */
            let configuration = ARWorldTrackingConfiguration()

            guard ARWorldTrackingConfiguration.isSupported else {
                fatalError(""
                    ARKit is not available on this device."")
            }
            sceneView.session.run(configuration)
            sceneView.delegate = self
            if let scene = SKScene(fileNamed: "Scene"){
                self.scene = scene as! Scene
                sceneView.presentScene(self.scene)
            } else {
                print("Error: scene initalisation failed")
            }
            let overflow = ((aRLocalDate?.debugDescription.count)! - 11)
            let endIndex = aRLocalDate?.debugDescription.index((aRLocalDate?.debugDescription.endIndex)!, offsetBy: -overflow)
            if let truncatedDate = aRLocalDate?.debugDescription.substring(to: endIndex!){
                DateLabel.text = truncatedDate
            }  
        }

        **/// - Tag: PlaceARContent**
        func view(_ view : ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {
            if self.Generated == false{
                self.guideLabel.alpha = 0
                parentNode = SKShapeNode(rectOf: CGSize(width: 400, height: 720))
                var count = 1;
                for time in timesArray {
                    **//add a SKSpriteNode and assign image to it**
                    **let labelNode : SKLabelNode = SKLabelNode(text: time)**
                    labelNode.name = "booklabel" + String(count)
                    labelNode.isUserInteractionEnabled = false;
                    parentNode?.addChild(labelNode)
                    posy -= 60
                    count += 1
                }
                parentNode?.alpha = 0.6
                self.Generated = true

                drawEventNodes()
                return parentNode
            }
            else {
                return nil
            }
        }

        //check if the booking is not in the past
        func checkIfValidTime(bookingTime: String, startDateTimeDate: Date) -> Bool {
            thisDate = ARViewController.dateToUse;
            let date = Date()
            let currentHour = Calendar.current.component(.hour, from: date)
            if (startDateTimeDate > date) {
                print("Start time is greater than the current date. = valid")
               **self.guideLabel.text = "Test"**
                return true;
            }
            else {
                print("Start time is not valid - before current time")
                **self.guideLabel.text = "Test"**
                return false;
            }
        }

        func doPost(bookingTime: String) {
            print("Start of post method")
            thisDate = ARViewController.dateToUse;
            roomToBook = globalVariables.roomDictionary[globalVariables.userString]!
            let name = globalVariables.userString;
            let date = Date()
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd"
            let displayName = globalVariables.userString
            let startDateStr = dateFormatter.string(from: thisDate!)
            let startHourString = bookingTime
            print("StartDateStr:", startDateStr)
            dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
            let startDateTimeString = "\(startDateStr)T\(startHourString)"
            let startDateTimeDate = dateFormatter.date(from: startDateTimeString)
            let endDateTimeDate = startDateTimeDate?.addingTimeInterval(3600)//3600 = 1 hour
            let endDateTimeString = dateFormatter.string(from: endDateTimeDate!)
            print("Start Date Time String", startDateTimeString)
            print("End date time string", endDateTimeString)
            print ("room to book: ",roomToBook)

            let valid = checkIfValidTime(bookingTime: bookingTime, startDateTimeDate: startDateTimeDate!)

            if (valid == true) {
                let jsonObject: [String: Any] =
                    [
                        "subject": "Booking",
                        "body":[
                            "contentType": "HTML",
                            "content": "Test Booking"
                        ],
                        "start":[
                            "dateTime": startDateTimeString,
                            "timeZone": "UTC"
                        ],
                        "end": [
                            "dateTime": endDateTimeString,
                            "timeZone": "UTC"
                        ],
                        "location":[
                            "displayName": displayName
                        ],
                        "attendees":[[
                            "emailAddress": [
                                "address": roomToBook,
                                "name": displayName
                            ],

                            "type": "required"
                            ]]
                ]

                //let valid = JSONSerialization.isValidJSONObject(jsonObject) // true
                let jsonData = try? JSONSerialization.data(withJSONObject: jsonObject)

                // create post request
                let url = URL(string: "https://graph.microsoft.com/v1.0/me/events")!

                var request = URLRequest(url: url)
                request.setValue("Bearer \(globalVariables.accessToken)", forHTTPHeaderField: "Authorization")
                request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
                request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")        // the expected response is also JSON
                request.httpMethod = "POST"

                // insert json data to the request
                request.httpBody = jsonData

                let task = URLSession.shared.dataTask(with: request) { data, response, error in
                    guard let data = data, error == nil else {
                        print(error?.localizedDescription ?? "No data")
                        return
                    }
                    let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
                    if let responseJSON = responseJSON as? [String: Any] {
                        print(responseJSON)
                    }
                }

                task.resume()
                print("Post Done")
                print("Refreshing now")
                //code to refresh?

            }
            else {
                print("Invalid booking time - it is in the past.")
            }
        }    
场景。斯威夫特:

class Scene : SKScene{
    var controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ARStoryBoard") as! ARViewController
   // var controller: ARViewController!
    var bookingTime : String?

    override func touchesBegan(_ touches: Set<UITouch>, with event : UIEvent?) {
       // var c =  self.view?.window?.rootViewController as! ARViewController;

        for touch in touches {
            let location = touch.location(in: self)
            let node : SKNode = self.atPoint(location)
            let name = node.name
            switch name {
            case "booklabel1"?:
                controller.doPost(bookingTime: "08:00:00")
            case "booklabel2"?:
                controller.doPost(bookingTime: "09:00:00")
            case "booklabel3"?:
                controller.doPost(bookingTime: "10:00:00")
            case "booklabel4"?:
                controller.doPost(bookingTime: "11:00:00")
            case "booklabel5"?:
                controller.doPost(bookingTime: "12:00:00")
            case "booklabel6"?:
                controller.doPost(bookingTime: "13:00:00")
            case "booklabel7"?:
                controller.doPost(bookingTime: "14:00:00")
            case "booklabel8"?:
                controller.doPost(bookingTime: "15:00:00")
            case "booklabel9"?:
                controller.doPost(bookingTime: "16:00:00")
            case "booklabel10"?:
                controller.doPost(bookingTime: "17:00:00")
            case "booklabel11"?:
                controller.doPost(bookingTime: "18:00:00")
            default:
                print ("No Specific Label Clicked")
            }
        }
    }

}
类场景:SKScene{
var controller=UIStoryboard(名称:“Main”,bundle:nil)。实例化eViewController(标识符为:“ARStoryBoard”)为!ARViewController
//变量控制器:ARViewController!
var bookingTime:字符串?
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
//var c=self.view?.window?.rootViewController作为!ARViewController;
接触{
让位置=触摸。位置(in:self)
let节点:SKNode=self.atPoint(位置)
让name=node.name
交换机名称{
“图书标签1”的案例:
控制器。doPost(预订时间:“08:00:00”)
“图书标签2”一案:
控制器.doPost(预订时间:“09:00:00”)
“图书标签3”一案:
controller.doPost(预订时间:“10:00:00”)
“书柜4”一案:
controller.doPost(预订时间:“11:00:00”)
“图书标签5”的案例:
controller.doPost(预订时间:“12:00:00”)
“图书标签6”一案:
controller.doPost(预订时间:“13:00:00”)
“图书标签7”的案例:
controller.doPost(预订时间:“14:00:00”)
“图书标签8”一案:
controller.doPost(预订时间:“15:00:00”)
“图书标签9”一案:
controller.doPost(预订时间:“16:00:00”)
“图书标签10”的案例:
控制器.doPost(预订时间:“17:00:00”)
“图书标签11”一案:
controller.doPost(预订时间:“18:00:00”)
违约:
打印(“未单击特定标签”)
}
}
}
}
试试这个。(检查更新的编辑答案:强烈推荐)

ARViewController:在类外部创建实例

weak var arViewControllerInstance = ARViewController()
确保ARViewController类内的初始化:

 override func viewDidLoad() {
            super.viewDidLoad()
            arViewControllerInstance = self
        }
现在,您可以使用以下命令调用Scene.Swift:

arViewControllerInstance?.doPost(bookingTime: "08:00:00")
已编辑:强烈推荐

上述方法简单,但不推荐用于最佳实践。请看下面使用委托协议的实现

创建协议

protocol ScenceArViewControllerDelegate {
    func doPost(bookingTime: String)
}
在ARViewController类中添加上述委托,如下所示

class ARViewController: UIViewController, ScenceArViewControllerDelegate{
    func doPost(bookingTime: String){
       //Funcion body goes here
    }
}
在场景类中创建代理变量,如下所示(Scene.swift)

一旦你实现了这一切。现在您已经从
ARViewController
声明了
delegateARVC
变量,如下代码所示。(注意:您可以将依赖项注入用于设置值,但下面仅设置对象)

现在一切都好了。现在,通过使用
scenearviewcontrollerdelegate
调用
doPost
的方法,场景类知道它与
ARViewController
有一个关系引用

您可以从场景类调用
AARViewController
doPost
方法,如下所示

guard let delegateARCAvailable = delegateARVC else { return }
delegateARCAvailable.doPost(bookingTime: "08:00:00")

你想申请什么就申请什么。谢谢。

@Liam这里有“所有权链”。 “自己”是指负责使物体保持活力。示例库、读者和书籍。这本书有图书馆的图章,所以有参考价值,但这本书肯定并没有图书馆。读者也一样。但图书馆和读者都声称拥有这本书。若并没有人带书去阅读(声明所有权),图书馆也会被毁——他们的书也会被毁

在Swift中,它通过
strong
引用实现。默认情况下,所有变量都是
strong

通常,您可以查看由应用程序委托或其他视图控制器(手动或使用情节提要)实例化的控制器。其他任何人都可以被视图控制器使用,并且可以知道viewcontroller,但不“拥有”它。所以变量必须声明为
。请注意,您并不拥有
,因此它必须是可选的,并且可以在任何时候取消(如果用户返回),因此您必须使用额外的防护

总结如下:

1:将场景中的viewController声明为弱:

class Scene : SKScene{
    weak var controller: ARViewController?
    ....*
}
2:为场景提供视图控制器。可以在创建场景或设置场景时执行此操作。例如:

 class ARViewController: UIViewController, ARSKViewDelegate, URLSessionDelegate {
 //...
 var scene : Scene? {
    didSet{
       //Optional: In case you can change scenes - remove view controller from old scene 
       oldValue?.controller = nil
       //Actually set view controller of any scene it "own"
       scene?.controller = self
    }
 }

PS:还有一个附加的修饰符“unowned”。但这是一种更先进的技术,可能会导致问题和崩溃。我建议您熟悉
。习惯于
保护
保留循环,然后继续

这起作用了。为什么我不应该使用这种方法@MichaelVorontsov@PranavanSp您认为使用协议/代理来做我想做的事情是一个好主意吗?如果是,你能举个例子吗
class Scene : SKScene{
    weak var controller: ARViewController?
    ....*
}
 class ARViewController: UIViewController, ARSKViewDelegate, URLSessionDelegate {
 //...
 var scene : Scene? {
    didSet{
       //Optional: In case you can change scenes - remove view controller from old scene 
       oldValue?.controller = nil
       //Actually set view controller of any scene it "own"
       scene?.controller = self
    }
 }