Ios Xcode多个UIButton操作

Ios Xcode多个UIButton操作,ios,xcode,swift,uibutton,Ios,Xcode,Swift,Uibutton,我一直在尝试编写一个soundboard应用程序,其中当然有多个声音,到目前为止,我有两个按钮和两个声音,但每个按钮播放相同的声音。我是一个noob程序员,所以请详细解释我需要做什么。对于我的格式设置,我深表歉意,因为这是我第一个被问及堆栈溢出的问题,下面的所有内容都是代码。目前我的故事板上有两个按钮。我们将不胜感激。下面是我的ViewController.swift import UIKit import AVFoundation class ViewController: UIViewC

我一直在尝试编写一个soundboard应用程序,其中当然有多个声音,到目前为止,我有两个按钮和两个声音,但每个按钮播放相同的声音。我是一个noob程序员,所以请详细解释我需要做什么。对于我的格式设置,我深表歉意,因为这是我第一个被问及堆栈溢出的问题,下面的所有内容都是代码。目前我的故事板上有两个按钮。我们将不胜感激。下面是我的ViewController.swift

import UIKit 
import AVFoundation

class ViewController: UIViewController {
    var sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button", ofType: "wav")!)
    var sound2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sweg", ofType: "wav")!)
    var audioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func soundbutton(sender: UIButton) {
       audioPlayer.play()
    }

    @IBAction func sound2(sender: UIButton) {
       audioPlayer.play()
    }
}    

您需要用另一种声音初始化另一个AVAudioPlayer,并在第二次点击按钮时播放。

您应该对这些按钮使用单个iAction

@IBAction func soundButton (sender: UIButton){
 switch sender.currentTitle! {
   case "sound1"://here you put the title the first button has
      audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
      audioPlayer.play()
   case "sound2"://here you put the title the second button has,here sound2 was an example,because IDK your buttons titles
      audioPlayer = AVAudioPlayer(contentsOfURL: sound2, error: nil)
      audioPlayer.play()
   default : break    
   }
 }

currentTitle是按钮的标题,因此,如果第一个按钮标题是在view Dod load后删除该行,只是在点击按钮时导致我的应用程序崩溃,那么您的第一个解决方案给了我一个错误:“二进制运算符”~=“不能应用于”case“上的“String”和“String”类型的操作数“lines.check the Screenshot在加载视图后删除该行只会导致我的应用程序在按下按钮时崩溃,您的第一个解决方案给了我一个错误:“二进制运算符”~=”不能应用于“case”上的“String”和“String”类型的操作数“行。我告诉过你的只是一个解决方案。在iOS模拟器中使用建议的代码运行应用程序会导致其挂起在启动屏幕上,并显示错误:“致命错误:在展开可选值时意外发现零”在第一行var sound上。使用新代码??您的按钮名为sound1和sound2。如果您只是复制粘贴??我会收到一个应用程序崩溃,看起来它是由我的应用程序委托引起的。“class AppDelegate:UIResponder,UIApplicationLegate{”标记为绿色,并带有“Thread 1:signal SIGABRT”字样。请您更具体一点,好吗?我不明白为什么您没有删除这些函数(viewDidLoad和func didReceiveMemoryWarning),为什么要做两个iAction,而你只需要一个,如果你只为一个按钮使用一个iAction,为什么你还有一个按钮呢?是的,一个按钮也可以。但是他说他是noob程序员。所以这对他们来说是简单而更好的解决方案。我想他会想添加更多的按钮,最好添加一个在另一个开关的情况下,然后是一个新的func,代码将看起来像块。我不明白你是什么意思“是的,它可以做一个按钮”,比我提到的iAction要复杂得多,如果你做了数千次iAction,为什么你仍然有一个论点,让事情变得更复杂??我过去常常编写提供的代码,但每当按下一个按钮,它就会导致我的应用程序崩溃。
import UIKit 
import AVFoundation

class ViewController: UIViewController {
    var sound = NSURL(fileURLWithPath:     NSBundle.mainBundle().pathForResource("button", ofType: "wav")!)   
   var sound2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sweg", ofType: "wav")!)
    var audioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
         }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }

    @IBAction func soundbutton(sender: UIButton) {
audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
       audioPlayer.play()
    }

    @IBAction func sound2(sender: UIButton) {
audioPlayer = AVAudioPlayer(contentsOfURL: sound2, error: nil)
       audioPlayer.play()
    }
}