我已经开始构建一个非常简单的应用程序,它有两个屏幕。第一个有两个UI按钮。一个按钮在我的第一个视图控制器(ViewController.swift)中使用下面的代码播放随机声音,另一个按钮以模态方式呈现第二个屏幕。
在第二个屏幕上还有两个UI按钮,一个用于播放另一个随机声音,另一个用于返回第一个屏幕。
在第一个屏幕上播放随机声音效果非常好。
对于第二个屏幕,我创建了一个名为PageOneViewController.swift的全新ViewController,并确保在IB中正确分配了类。第二个视图控制器包含来自第二页的代码的副本克隆,除了名称的更改以避免冲突和混淆。但是,在第二页上播放声音的任何尝试都会导致无法识别的选择器错误,我无法弄清楚原因。
错误消息如下:
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()
}
}
答案 0 :(得分:1)
好吧,我仍然不知道究竟是什么导致了这个问题,但是我不顾一切地尝试修复它导致我从2.3更新到Swift 3,立即修复了问题。
答案 1 :(得分:0)
可能发生的事情是在故事板中,可能有一个双重引用,例如引用两个IBOutlet或两个IBActions的按钮。您可能忘记删除它们中的引用,在连接检查器中检查它们的连接,或者单击其中一个。您是否尝试在代码或断点中放置print语句以确切地查看它崩溃的位置?