我已经开始构建一个具有两个屏幕的非常简单的应用程序。第一个有两个UIbutton。一个按钮在我的第一个 View Controller (ViewController.swift)中使用以下代码播放随机声音,另一个按钮以模态显示第二个屏幕。

在第二个屏幕上,还有两个UIbutton,一个用于播放另一种随机声音,另一个用于返回第一个屏幕。

在第一个屏幕上播放随机声音效果完美。

对于第二个屏幕,我创建了一个名为PageOneViewController.swift的全新ViewController,并确保在IB中正确分配了该类。第二个 View Controller 包含第二页代码的复本克隆,除了更改名称以避免冲突和混淆之外。但是,在此第二页上播放声音的任何尝试都会导致无法识别的选择器错误,我无法弄清原因。

错误消息如下:

2016-10-17 18:04:57.749 NewApp[28750:2719569] *** Terminating app     due to uncaught exception 'NSInvalidArgumentException', reason: '-    [NewApp.PageOneViewController buttonPressed2WithSender:]: unrecognized     selector sent to instance 0x7fe79070b8a0'

它停在这条线上(我意识到这没有多大帮助):
class AppDelegate: UIResponder, UIApplicationDelegate {

我通过检查器手动连接按钮,并使用“Touch Up Inside”作为触摸方法。

我根本无法解决这个问题。在旧的Objective-C美好的日子里,当我错误地命名文件或其中一个声音文件丢失时,我会遇到这个问题,但是我一遍又一遍地检查了所有内容,无法解决问题。

ViewController.swift的代码:
import AVFoundation
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var mainButton1: UIButton!

    var arrayOfSounds = ["1", "2", "3", "4", "5", "6", "7", "8", "9",     "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",     "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33",     "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44",
                         "45", "46", "47", "48", "49", "50", "51" ,     "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63",     "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75",     "76", "77", "78", "79", "80", "81", "82"]
    var audioPlayer : AVAudioPlayer?

    func setupAudioPlayer(file: NSString, type: NSString){
        let path = NSBundle.mainBundle().pathForResource(file as     String, ofType: type as String)
        let url = NSURL.fileURLWithPath(path!)
        do {
            try  audioPlayer =  AVAudioPlayer(contentsOfURL: url)
        } catch {
            print("Player not available")
        }
    }

    @IBAction func buttonPressed(sender: AnyObject){
            let range: UInt32 = UInt32(arrayOfSounds.count)
            let number = Int(arc4random_uniform(range))

    self.setupAudioPlayer(arrayOfSounds[number], type: "wav")
    self.audioPlayer?.play()




}
}

PageOneViewController.swift的代码:
import AVFoundation
import UIKit

class PageOneViewController: UIViewController {

    @IBOutlet weak var mainButtonNew: UIButton!

    var arrayOfSounds = ["1", "2", "3", "4", "5", "6", "7", "8", "9",     "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",     "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33",     "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44",
                         "45", "46", "47", "48", "49", "50", "51" ,     "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63",     "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75",     "76", "77", "78", "79", "80", "81", "82"]
    var audioPlayer : AVAudioPlayer?

    func setupAudioPlayer(file: NSString, type: NSString){
        let path = NSBundle.mainBundle().pathForResource(file as     String, ofType: type as String)
        let url = NSURL.fileURLWithPath(path!)
        do {
            try  audioPlayer =  AVAudioPlayer(contentsOfURL: url)
        } catch {
            print("Player not available")
        }
    }

    @IBAction func buttonPressedNew(sender: AnyObject){
            let range: UInt32 = UInt32(arrayOfSounds.count)
            let number = Int(arc4random_uniform(range))

    self.setupAudioPlayer(arrayOfSounds[number], type: "wav")
    self.audioPlayer?.play()




}
}

最佳答案

可能发生的情况是在 Storyboard 中,某些内容可能具有双重引用,例如引用两个IBOutlet或两个IBAction的按钮。您可能已经忘记从它们中删除引用,请在连接检查器中或单击ctrl来检查它们的连接。您是否尝试过在代码或断点中放置打印语句以查看其崩溃的确切位置?

关于ios - 在新的ViewController中使用代码时,UIButton无法识别的选择器发送到实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40092279/

10-14 20:46
查看更多