gpt4 book ai didi

ios - 只有实例方法可以声明@IBAction

转载 作者:行者123 更新时间:2023-11-30 11:51:05 29 4
gpt4 key购买 nike

我对 Xcode 和编码都很陌生,所以请耐心等待,哈哈。我正在为一个科学博览会项目开发我的第一个应用程序,该应用程序基于选项卡,有两个主页。一页包含多个开关;无论哪个开关打开,都决定其他页面上的大按钮播放什么声音或动画。

在查看了几个教程并创建了 Storyboard后,我开始编写将操作连接到按钮和开关的部分。我在具有 pushButton 函数的行上继续收到以下错误:Only instance method can beelated @IBAction

我检查了我的括号和方括号,我不认为有错误,但由于我是初学者,我可能会遗漏一些东西。我的代码粘贴在下面,感谢任何帮助。一旦我解决了这个问题,空开关功能将像第一个一样。 :)

import UIKit
import AVFoundation
import AVKit

class FirstViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
var CatSound = URL(fileURLWithPath: Bundle.main.path(forResource: "CatSound", ofType: "mp3")!)

//Mark: Properties
@IBOutlet var pushButton: UIButton!

@IBAction func meowSwitch(_ sender: UISwitch) {
while (sender.isOn == true) {
@IBAction func pushButton(_ sender: UIButton) {
let CatSound = URL(fileURLWithPath: Bundle.main.path(forResource: "CatSound", ofType: "mp3")!)
do {
audioPlayer = try AVAudioPlayer(contentsOf: CatSound)
audioPlayer.prepareToPlay()
} catch {
print("Problem in getting File")
}
audioPlayer.play()
}
}

}

@IBAction func barkSwitch(_ sender: UISwitch) {
}

@IBAction func carSwitch(_ sender: UISwitch) {
}

@IBAction func trainSwitch(_ sender: UISwitch) {
}

@IBAction func helloSwitch(_ sender: UISwitch) {
}

@IBAction func applauseSwitch(_ sender: UISwitch) {
}

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 meowSwitch(_ sender: UISwitch) {
while (sender.isOn == true) {
@IBAction func pushButton(_ sender: UIButton) {
.....
}
}
}

您不能在另一个函数中声明一个函数。

您应该创建一个单独的函数 func playMeowSound(){...} ,其中的代码位于 pushButton(_ sender: UIButton) 中,然后执行以下操作:

@IBAction func meowSwitch(_ sender: UISwitch) {
while (sender.isOn == true) {
playMeowSound()
}
}

关于ios - 只有实例方法可以声明@IBAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48308784/

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