“无法识别的选择器发送到实例"添加ViewController时出错 [英] "unrecognized selector sent to instance" error when adding ViewController

查看:40
本文介绍了“无法识别的选择器发送到实例"添加ViewController时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试将欢迎屏幕添加到用作主屏幕的AR应用程序中.应用加载后,用户可以点击按钮,然后应用冻结,崩溃并显示代码

I've recently attempted to add a welcome screen to my AR app that works as a Home screen. When the app loads, the user can tap the button and then the app freezes, crashes and displays the code

 "Thread 1: "-[_0_2_2020_2.WelcomeViewController letsGo:]: unrecognized selector sent to instance 0x13ec05e00"

  • 我已经尝试了一些可用的解决方案,但是我还无法提出解决方案.我认为这与我的* IBAction连接有关.非常感谢您的协助!

    • I've tried a few of the solutions available, but I haven't been able to come up with a solution. I think it has something to do with my *IBAction connection. Any assistance is greatly appreciated!

       import UIKit
       import RealityKit
       import ARKit
      
       class WelcomeViewController: UIViewController {
      
      
      
        @IBAction func gotPressed(_ sender: Any) {let storyboard = UIStoryboard(name: "Main", 
        bundle: nil)
           if let viewController = storyboard.instantiateViewController(withIdentifier: 
       "ViewController") as? ViewController {
            self.present(viewController, animated: true, completion: nil) /// present the view 
       controller (the one with the ARKit)!
        }    }
      
      
      }
      
      
      
        class ViewController: UIViewController, ARSessionDelegate {
           //delay app launch to show splash screen
          func application(_ application: UIApplication, didFinishLaunchingWithOptions 
        launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
              Thread.sleep(forTimeInterval: 3.0)
              // Override point for customization after application launch.
              return true
         }
       //end splash screen delay
       @IBOutlet var arView: ARView!
      
        override func viewDidLoad() {
        super.viewDidLoad()
      
        arView.session.delegate = self
      
        showModel()
        overlayCoachingView()
        setupARView()
      
        arView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:))))
      
        }
      
      func showModel(){
      
        let anchorEntity = AnchorEntity(plane: .horizontal, minimumBounds:[0.7, 0.7])
      
        anchorEntity.scale = [0.2, 0.2, 0.2]
      
          let entity = try! Entity.loadModel(named: "COW_ANIMATIONS")
          entity.setParent(anchorEntity)
      
          arView.scene.addAnchor(anchorEntity)
      
      
        }
      //Overlay coaching view "adjust iphone scan"
      func overlayCoachingView () {
      
        let coachingView = ARCoachingOverlayView(frame: CGRect(x: 0, y: 0, width: arView.frame.width, height: arView.frame.height))
      
        coachingView.session = arView.session
        coachingView.activatesAutomatically = true
        coachingView.goal = .horizontalPlane
      
        view.addSubview(coachingView)
      
      }//end overlay
      
      
      func setupARView(){
        arView.automaticallyConfigureSession = false
        let configuration = ARWorldTrackingConfiguration()
        configuration.planeDetection = [.horizontal, .vertical]
        configuration.environmentTexturing = .automatic
        arView.session.run(configuration)
      }
      
      //object placement
      
      @objc
      func handleTap(recognizer: UITapGestureRecognizer){
        let location = recognizer.location(in:arView)
      
        let results = arView.raycast(from: location, allowing: .estimatedPlane, alignment: .horizontal)
      
        if let firstResult = results.first {
            let brownCowAnchor = ARAnchor(name: "COW_ANIMATIONS", transform: firstResult.worldTransform)
            arView.session.add(anchor: brownCowAnchor)
        } else {
            print("Object placement failed - couldn't find surface.")
      
      
      
            //cow animations
            //let robot = try! ModelEntity.load(named: "COW_ANIMATIONS")
           let brownCowAnchor = AnchorEntity()
            let blackCowAnchor = AnchorEntity()
      
            //anchor.children.append(robot)
            //arView.scene.anchors.append(anchor)
      
            //robot.playAnimation(robot.availableAnimations[0].repeat(duration: .infinity),
                                                          //transitionDuration: 0.5,
                                                                //startsPaused: false)
      
            //start cow animation
      
      
            let brownCow = try! ModelEntity.load(named: "COW_ANIMATIONS")
            let blackCow = try! ModelEntity.load(named: "Cow")
      
            brownCow.position.x = -1.0
            blackCow.position.x = 1.0
            brownCowAnchor.position.z = -2.0
            blackCowAnchor.position.z = -2.0
            brownCow.setParent(brownCowAnchor)
            blackCow.setParent(blackCowAnchor)
            arView.scene.anchors.append(brownCowAnchor)
            arView.scene.anchors.append(blackCowAnchor)
      
            let cowAnimationResource = brownCow.availableAnimations[0]
            let horseAnimationResource = blackCow.availableAnimations[0]
      
            brownCow.playAnimation(cowAnimationResource.repeat(duration: .infinity),
                                                transitionDuration: 1.25,
                                                      startsPaused: false)
      
            blackCow.playAnimation(horseAnimationResource.repeat(duration: .infinity),
                                                    transitionDuration: 0.75,
                                                          startsPaused: false)
      
            //end cow animations
        }
      }
      
      func placeObject(named entityName: String, for anchor: ARAnchor)  {
        let entity = try! ModelEntity.loadModel(named: entityName)
      
        entity.generateCollisionShapes(recursive: true)
        arView.installGestures([.rotation, .translation], for: entity)
      
      
        let anchorEntity = AnchorEntity(anchor: anchor)
        anchorEntity.addChild(entity)
        arView.scene.addAnchor(anchorEntity)
      
      
      }
      }
      

    • 推荐答案

      该按钮正在触发功能 letsGo ,该功能不会出现在您发布的 WelcomeViewController 上的任何地方.检查界面生成器,并确保已从按钮中删除了旧的连接.应该是最后一个标签.

      The button is triggering a function letsGo which doesn't appear anywhere on the WelcomeViewController you posted. Check interface builder and make sure that you've removed the old connection from the button. Should be the final tab.

      这篇关于“无法识别的选择器发送到实例"添加ViewController时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆