【问题标题】:Xcode Multiple UIButton ActionsXcode 多个 UIButton 操作
【发布时间】:2015-04-18 14:41:00
【问题描述】:

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

【问题讨论】:

  • 在视图加载后删除该行只会导致我的应用程序在点击按钮时崩溃,您的第一个解决方案给了我一个错误:“二进制运算符'~='不能应用于'类型的操作数String' 和 'String?'" 在 "case" 行上。
  • 查看截图

标签: ios xcode swift uibutton


【解决方案1】:

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

@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 中删除该行

编辑:在类中这就是你在 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,因为该按钮有一些出口和操作

【讨论】:

  • 在视图加载后删除该行只会导致我的应用程序在点击按钮时崩溃,您的第一个解决方案给了我一个错误:“二进制运算符'~='不能应用于'类型的操作数String' 和 'String?'" 在 "case" 行上。
  • 我告诉你的只是一种解决方案。
  • 在 iOS 模拟器中使用建议的代码运行应用程序会导致它挂在启动屏幕上,并在第一个 var 上显示错误:“致命错误:在展开可选值时意外发现 nil”声线。
  • 用新代码??你的按钮被命名为sound1和sound2。如果你只是复制粘贴??
  • 我遇到了一个应用程序崩溃,这看起来是由我的应用程序委托引起的。 “class AppDelegate: UIResponder, UIApplicationDelegate {” 用“Thread 1: signal SIGABRT”字样标记为绿色
【解决方案2】:

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

【讨论】:

  • 能否说得更具体一些?
【解决方案3】:
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()
    }
}    

【讨论】:

  • 我不明白你为什么没有删除那些 func(viewDidLoad 和 func didReceiveMemoryWarning),以及为什么要做 2 个 IBAction,当你只需要一个时,如果你只使用一个 IBAction按钮,为什么你还有 sender 参数
  • 是的,一个按钮就可以做到。但他说他是新手程序员。所以这对他们来说是简单而更好的解决方案。
  • 我在想他会想要添加更多按钮,最好再添加一个开关盒,然后再添加一个新功能,代码会看起来像块状。我不明白是什么你的意思是“是的,可以用一个按钮来做”,而不是我提到的 IBAction,如果你做了数千个 IBAction,为什么你仍然有争论,让事情变得更复杂??
  • 我曾经提供过代码,但它会导致我的应用在按下按钮时崩溃。
猜你喜欢
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多