所以我已经看到并根据我在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)
}
}
答案 0 :(得分:1)
您从哪里获得audioDefaultIsMuted,并检查其值。
你可以使用swift let if来简化你的代码
if let value = audioDefaultIsMuted.value(forKey: "Volume") {
print(value)
}
答案 1 :(得分:0)
可能是您已将按钮操作连接到方法,之后您更改了方法名称或删除了方法并编写并连接了新方法。
这样做 -