我有静态API函数,它是 void 并调用外部API,现在我想检查响应,当它是 nil 时,我想禁用该按钮并更改其颜色。
但是在Alamofire中,即使使用self也无法访问按钮属性。
@IBOutlet weak var AudioButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// self.AudioButton.tintColor=UIColor.gray work here
if(gameData?.WordId != nil){
APIClient.getWordAudio(id: gameData!.WordId)
.execute(onSuccess: { response in
DatabaseManager.shared.saveWordAudios([response])//
let audioData = NSData(base64Encoded: response.content!, options: .ignoreUnknownCharacters)
// here I want to check for nil and then disable the button
if(response.content==nil){
// this two line dosen't work inside this closure
self.AudioButton.tintColor=UIColor.gray
self.AudioButton.isEnabled=false;
}
}, onFailure: { error in
print("[WordViewController] \(error)")
}
)
}
}
最佳答案
请显示AudioButton的声明。我认为您正在访问类AudioButton
而不是其对象。
let audioButton: AudioButton?
然后从闭包内部访问该对象。
if(response.content==nil){
self.audioButton.tintColor=UIColor.gray
self.audioButton.isEnabled=false;
}