因此,根据我在 stackoverflow 上看到的内容,我已经看到并尝试对我的代码进行无数次更改,但实际上似乎没有任何效果。当我按下音量按钮时会发生错误,该按钮将写入用户默认数据,因此我可以检查在实际游戏过程中何时播放音频。
我认为这只是我的问题,因为我已经尝试了所有方法。不管怎样,我的代码如下:
//if user presses volume button
@IBAction func DaVol(_ sender: Any)
{
//if User default for audio is nil, initialize it, otherwise change it where needed
if audioDefaultIsMuted.value(forKey: "Volume") != nil
{
//if the audio has been on turn it off
let tempAudioDefaultIsMuted = audioDefaultIsMuted.value(forKey: "Volume") as! Bool
if tempAudioDefaultIsMuted == false
{
//if label is the on music one, set value to true (meaning ismuted will be true)
//then change label to off label
audioDefaultIsMuted.set(true, forKey: "Volume")
audioDefaultIsMuted.synchronize()
//change to new image
volumectrl.setImage(#imageLiteral(resourceName: "nowaves"), for: UIControlState.normal)
}
else
{
//otherwise set value to false
//then change label to on one
audioDefaultIsMuted.set(true, forKey: "Volume")
audioDefaultIsMuted.synchronize()
//change to new image
volumectrl.setImage(#imageLiteral(resourceName: "audio"), for: UIControlState.normal)
}
}
let currVolImage = volumectrl.image(for: UIControlState.normal)
//check curr image and do same shit as before accordingly
if currVolImage == #imageLiteral(resourceName: "audio")
{
//if label is the on music one, set value to true (meaning ismuted will be true)
//then change label to off label
audioDefaultIsMuted.set(true, forKey: "Volume")
audioDefaultIsMuted.synchronize()
volumectrl.setImage(#imageLiteral(resourceName: "nowaves"), for: UIControlState.normal)
}
else if currVolImage == #imageLiteral(resourceName: "nowaves")
{
//otherwise set value to false
//then change label to on one
audioDefaultIsMuted.set(false, forKey: "Volume")
audioDefaultIsMuted.synchronize()
volumectrl.setImage(#imageLiteral(resourceName: "audio"), for: UIControlState.normal)
}
}
最佳答案
您从哪里获得 audioDefaultIsMuted,并检查其值。
你可以像这样使用 swift let if 来简化你的代码
if let value = audioDefaultIsMuted.value(forKey: "Volume") {
print(value)
}
关于ios - 无法识别的选择器发送到实例 0x7fe098c09830,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42311486/