gpt4 book ai didi

ios - Xcode 多个 UIButton Action

转载 作者:行者123 更新时间:2023-11-28 07:08:04 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com