我有一个视图控制器,显示带有外来单词的标签。它有4个按钮,每个按钮有3个随机字和1个与外来字匹配的字。单击其中一个按钮后,它需要转换到底部的2个视图控制器中的1个。如果您单击的按钮与标签不匹配,则左侧的其中一个单词会显示该单词,右侧的视图控制器会显示您正确的单词并在随机选择的句子中显示该单词。
问题是我一直在谷歌上搜索如何正确地从一个按钮中截取多个视图,同时传递数据约5个小时,我无法找到解决方案。
:编辑我没有足够的声誉来发布图片。抱歉!这伤害很多
我尝试过制作一个复杂的prepareForSegue()函数,但是当按下每个按钮时,我没有办法调用它。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if ButtonAnswer1.description == testedWord {
if (segue.identifier == "segueToAnsweredCorrect") {
var acvc : AnsweredCorrectViewController = segue.destinationViewController as! AnsweredCorrectViewController;
println(testedWord)
println(randomSentenceToTransfer)
acvc.testedWord = testedWord
acvc.sentenceTransferred = randomSentenceToTransfer
}
if ButtonAnswer2.description == testedWord {
if (segue.identifier == "segueToAnsweredCorrect") {
var acvc : AnsweredCorrectViewController = segue.destinationViewController as! AnsweredCorrectViewController;
println(testedWord)
println(randomSentenceToTransfer)
acvc.testedWord = testedWord
acvc.sentenceTransferred = randomSentenceToTransfer
}
if ButtonAnswer3.description == testedWord {
if (segue.identifier == "segueToAnsweredCorrect") {
var acvc : AnsweredCorrectViewController = segue.destinationViewController as! AnsweredCorrectViewController;
println(testedWord)
println(randomSentenceToTransfer)
acvc.testedWord = testedWord
acvc.sentenceTransferred = randomSentenceToTransfer
}
if ButtonAnswer4.description == testedWord {
if (segue.identifier == "segueToAnsweredCorrect") {
var acvc : AnsweredCorrectViewController = segue.destinationViewController as! AnsweredCorrectViewController;
println(testedWord)
println(randomSentenceToTransfer)
acvc.testedWord = testedWord
acvc.sentenceTransferred = randomSentenceToTransfer
}
else {
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "segueToReviewCard") {
var rcvc : ReviewCardViewController = segue.destinationViewController as! ReviewCardViewController;
rcvc.translationReviewed = testedTranslation
rcvc.wordReviewed = testedWord
}
}
}
}
}
}
}
}
我已尝试制作一个与每个按钮一起使用的功能:
func performSegue(sender: UIButton) {
// checks if button's text matches the word tested variable's string
if sender.description == wordTested {
self.performSegueWithIdentifier(identifier: "segueToAnswerCorrect", sender: AnyObject?)
// I don't know how to pass data outside of prepareForSegue()
}
else {
self.performSegueWithIdentifier(identifier: "segueToReviewCard", sender: AnyObject?)
// pass data
}
}
我还不知道如何正确地处理协议和代理,我不确定这是否是解决方案。
如果您有任何相关信息,这将是一个救生员。
答案 0 :(得分:0)
你真的很亲密。只是对您的performSegue的一个小改动:
func performSegue(sender: UIButton) {
if sender.description == wordTested {
self.performSegueWithIdentifier(identifier: "segueToAnswerCorrect", sender: sender)
} else {
self.performSegueWithIdentifier(identifier: "segueToReviewCard", sender: sender)
}
}
然后将所有4个按钮连接到此功能。
然后你改变prepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "segueToAnsweredCorrect") {
var acvc : AnsweredCorrectViewController = segue.destinationViewController as! AnsweredCorrectViewController;
acvc.testedWord = testedWord
acvc.sentenceTransferred = randomSentenceToTransfer
}
if (segue.identifier == "segueToReviewCard") {
var rcvc : ReviewCardViewController = segue.destinationViewController as! ReviewCardViewController;
rcvc.translationReviewed = testedTranslation
rcvc.wordReviewed = testedWord
}
}
在界面构建器中,您需要设置两个segue,但没有用于按钮操作的segue。