“无法识别的选择器发送到实例"添加 ViewController 时出错 [英] "unrecognized selector sent to instance" error when adding 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 theWelcomeViewController
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屋!