【发布时间】:2017-05-15 10:48:20
【问题描述】:
我有一个场景显示数组中的一个问题。故事板包括两个按钮,可以转到下一个/上一个问题。我有一种方法可以向持有当前章节问题(Deck)的班级、当前问题和总问题数量询问。这用于更新标题。
我希望能够使用我的 updateTitle() 函数来控制我的两个按钮的启用状态 - 当当前问题是最后一个问题时禁用“下一个”按钮并禁用“上一个”按钮当显示第一个问题时。
我可以在其插座内更改按钮的状态,例如通过使用相关的 if 语句调用 sender.isEnable = false 。但是这样做的问题是,如果用户在按钮被禁用的点旁边单击,单击上一个按钮不会重新启用“下一个”按钮。
最好在 setTitle() 方法中设置按钮的状态?
这是我的课:
class QuestionController: UIViewController {
// OUTLETS //////
// Outlet for the 'question' label
@IBOutlet weak var questionTextView: UITextView!
// Outlet for the next button
@IBAction func nextQuestion(_ sender: UIButton) {
flashcard = deck.getNextCard()
questionTextView.text = flashcard!.question
self.updateTitle()
}
// Outlet for last button
@IBAction func lastQuestion(_ sender: UIButton) {
flashcard = deck.getLastCard()
questionTextView.text = flashcard!.question
self.updateTitle()
}
// PROPERTIES /////
var flashcard: Flashcard! // variable to hold current flashcard
var deck = Deck(chapter: 0) // Defaults to first chapter until set
// METHODS //////
// Update the title with progress
private func updateTitle() {
let (chapter, question, totalQuestions) = deck.getProgress()
self.title = "Chapter \(chapter) question \(question) of \(totalQuestions)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Holds deck of data
flashcard = deck.getCurrentCard()
questionTextView.text = flashcard!.question
self.updateTitle() // Update the title with progress
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Prepare flashcard item to be passed to the answer controller before segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let answerController = segue.destination as? AnswerController {
answerController.flashcard = flashcard
}
}
}
【问题讨论】:
-
点击下一个和上一个按钮时,检查问题的计数是否等于最后禁用下一个,如果是起始索引则禁用上一个。