我有一个答案数组和一个按钮数组。当我单击特定按钮时,在sender.tag的帮助下,我进行了索引更改。我还需要在override func prepare(for segue: UIStoryboardSegue, sender: Any?)
中进行操作,以便答案由索引设置,结果将转到另一个屏幕。
当我尝试创建sender.tag时出现错误
“任何类型”的值?没有成员“标签”
@IBAction func answerPressed(_ sender: UIButton) {
let index = sender.tag
let currentAnswer = currentQuestions?.answers[index]
if currentAnswer!.type != .next {
performSegue(withIdentifier: "segue", sender: sender)
} else {
currentQuestions = currentAnswer!.nextQuestion
updateTittles(currentQuestions)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let index = sender.tag
let currentType = currentQuestions?.answers[index].type
let resultViewController = segue.destination as! ResultViewController
resultViewController.type = currentType
}
答案 0 :(得分:4)
在这种情况下,用于准备segue功能的sender
不一定是UIButton
,它是发起segue的任何因素。您可以尝试将其转换为一个按钮,以查看它是否为按钮:
if let button = sender as? UIButton {
index = button.tag
}
您还可以在answerPressed
函数中执行操作,而不是执行segue,您可以在此处实例化ResultViewController
,然后将其实例化。因为当您使用此功能时,您知道发件人是UIButton
,并且应该有标签。
答案 1 :(得分:2)
任何任何都可以是sender
的类型,参数声明为Any
在成功执行后,首先检查segue的identifier
,将sender
参数下调为UIButton
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "segue" else { return }
let button = sender as! UIButton
let currentType = currentQuestions?.answers[button.tag].type
let resultViewController = segue.destination as! ResultViewController
resultViewController.type = currentType
}
但是不必传递整个UIButton
实例,标记/索引就足够了,或者-更有效的是-传递currentAnswer.type
@IBAction func answerPressed(_ sender: UIButton) {
let index = sender.tag
let currentAnswer = currentQuestions?.answers[index]
if currentAnswer!.type != .next {
performSegue(withIdentifier: "segue", sender: index)
} else {
currentQuestions = currentAnswer!.nextQuestion
updateTittles(currentQuestions)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "segue" else { return }
let index = sender as! Int
let currentType = currentQuestions?.answers[index].type
let resultViewController = segue.destination as! ResultViewController
resultViewController.type = currentType
}
答案 2 :(得分:1)
“任何类型”的值?没有成员“标签”
表示Mono<Long> clock = Mono.defer(() -> Mono.just(System.currentTimeMillis()));
//time == t0
Thread.sleep(10_000);
//time == t10
clock.block(); //invoked currentTimeMillis() here and returns t10
Thread.sleep(7_000);
//time == t17
clock.block(); //invoke currentTimeMillis() once again here and returns t17
对象实际上没有Any
属性。您知道您的tag
是sender
,所以为什么不将UIButton
中的sender
转换成prepareForSegue
对象呢? ?
UIButton