ios - Xcode 多个 UIButton Action

标签 ios xcode swift uibutton

我一直在尝试编写一个音板应用程序,其中当然有多种声音,到目前为止,我有两个按钮和两种声音,但每个按钮都播放相同的声音。我是菜鸟程序员,所以请向我详细解释我需要做什么。我为我的格式道歉,因为这是我被问到的第一个堆栈溢出问题,下面的所有内容都是代码。我的 Storyboard 上目前有 2 个按钮。帮助将不胜感激。下面是我的 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()
    }
}    

最佳答案

您应该为这些按钮使用单个 IBAction ex

@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 是按钮的标题,所以如果第一个按钮标题是😆,你用😆替换sound1。

没有笑话表情符号工作 :)):D

希望对你有帮助

编辑 你做错的是你在 viewDidload audioPlayer 中设置了声音,这就是为什么你只有一种声音

您可以从 viewDidLoad 中删除该行

Edit: inside the class 这就是你在 ViewController 中所需要的,仅此而已,仅此而已(),确保将按钮连接到 IBAction 并断开连接从其他方法

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

最简单的实现

import UIKit 
import AVFoundation

class ViewController: UIViewController {
 var audioPlayer = AVAudioPlayer()
 @IBAction func soundButton (sender: UIButton){
 if let title = sender.currentTitle{
 switch title {
    case "sound1"://here you put the title the first button has
       audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button", ofType: "wav")!), 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: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sweg", ofType: "wav")!), error: nil)
       audioPlayer.play()
     default : break    
    }
   }
 }
}

现在您遇到的唯一错误是 SIGABART,因为该按钮有一些输出和操作

关于ios - Xcode 多个 UIButton Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29718747/

相关文章:

ios - 构建 ios-app 错误域=IDEProvisioningErrorDomain 代码=9

ios - 从 Plist 中检索颜色。 iOS

ios - 在 Swift 类中访问 Objective C AppDelegate String 返回 nil

Swift 作为重载

ios - 在 ARkit SCNNODE 中的 3D 对象上重复 X 轴纹理 - SWIFT

ios - AirPlay图标因iOS版本而异

ios - Swift 2.0 和 Google Analytics : Use of unresolved identifier "GAI"

xcode - 自 Swift 2.2 以来无法访问 extern NSString

ios - 在 Swift 中对私有(private)变量进行单元测试

ios - SendBird iOS SDK 错误 14000, "Fail to start messaging"的可能原因是什么?