问题
@IBAction func Hint(_ sender: UIButton) {
if Action.currentTitle != Int(rightAnswerPlacement) // this is the line where I keep getting error I posted above
{
Action.isHidden = true // true to hide the button
}
}
@IBOutlet weak var Hint: UIButton!
//Label for qs
@IBOutlet weak var label: UILabel!
//button for choices
@IBOutlet weak var Action: UIButton!
@IBAction func Action(_ sender: AnyObject)
{
if (sender.tag == Int(rightAnswerPlacement))
{
print("Right")
}
else
{
print("Wrong")
let alert = UIAlertController(title: "That is incorrect", message: "Try again!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
present(alert, animated: true)
currentQuestion -= 1
//how to record which wrong answer the user is pressing
//let WrongAnswer = sender.tag.titleLabel?.text
}
This is a code for a basic quiz app on Xcode, what I am trying to do with my first few lines of code is trying to make an option disappear once I click another button
回答1:
Reason for the error is that both your button and a function are named Action. In the line that gives you error, compiler thinks you are referring to the function (which is of type (AnyObject) -> ()) instead of the button. Rename either your function, or your button.
By the way, there are couple of things you should improve in your code:
- by convention, names of types are uppercased, while names of instances should be lowercased
- use more descriptive names for your variables and functions - i.e.
hintButtoninstead ofHint,actionButtoninstead ofActionforUIButtonandactionButtonTappedforfunc
来源:https://stackoverflow.com/questions/63581325/i-am-not-sure-why-i-keep-getting-the-error-value-of-type-anyobject-has