【问题标题】:"unrecognized selector sent to instance" error when adding ViewController添加 ViewController 时出现“无法识别的选择器发送到实例”错误
【发布时间】:2020-10-09 15:17:21
【问题描述】:

我最近尝试在我的 AR 应用中添加一个欢迎屏幕,用作主屏幕。当应用加载时,用户可以点击按钮,然后应用冻结、崩溃并显示代码

 "Thread 1: "-[_0_2_2020_2.WelcomeViewController letsGo:]: unrecognized selector sent to instance 0x13ec05e00"
  • 我已经尝试了一些可用的解决方案,但我无法提出解决方案。我认为这与我的 *IBAction 连接有关。非常感谢任何帮助!

     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)
    
    
    }
    }
    

【问题讨论】:

    标签: swift xcode uiviewcontroller arkit realitykit


    【解决方案1】:

    该按钮正在触发一个函数letsGo,它不会出现在您发布的WelcomeViewController 的任何位置。检查界面生成器并确保您已从按钮中删除旧连接。应该是最后一个选项卡。

    【讨论】:

    • 请注意,错误表明它在 WelcomeViewController 中期待 letsGo: 而不是 ViewController
    • 是的。我应该更清楚。他发布了 WelcomeViewController 的代码。我会编辑并称之为
    猜你喜欢
    • 2012-04-04
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多