如何从插座外部访问按钮的属性 - Swift



我有一个场景,显示来自数组的问题。情节提要包括两个按钮,用于转到下一个/上一个问题。我有一个方法,可以询问持有当前章节、当前问题和总问题数的问题(Deck)的班级。这用于更新标题。

我希望能够使用我的 updateTitle() func 来控制我的两个按钮的启用状态 - 当当前问题是最后一个问题时禁用"下一个"按钮,并在显示第一个问题时禁用"上一个"按钮。

我能够从其出口内更改按钮的状态,例如通过调用 sender.isEnable = false 和一个相关的 if 语句。但这样做的问题是,如果用户单击按钮被禁用的点旁边,单击上一个按钮不会重新启用"下一步"按钮。

理想的做法是在 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
}
}
}

取按钮的出口使用它。诸如此类:-

class QuestionController: UIViewController {
@IBOutlet weak var questionTextView: UITextView!
@IBOutlet weak var btnNext: UIButton!
}

谢谢,我最终能够创建插座。然后,我使用下面的 if 语句更新了 updateTitle() 方法,该语句更新了相关点的按钮状态。

//
//  ViewController.swift
//  CISSP Flashcards
//
//  Created by Laurie Hocking on 24/04/2017.
//  Copyright © 2017 sQuidgy labs. All rights reserved.
//
import UIKit
class QuestionController: UIViewController {
// OUTLETS //////
// Outlet for the 'question' label
@IBOutlet weak var questionTextView: UITextView!
@IBOutlet weak var nextOutlet: UIButton!
@IBOutlet weak var lastOutlet: UIButton!
@IBOutlet weak var nextQuestion: UIButton!
// 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)"
if (question == 1) {
lastOutlet.isEnabled = false
nextOutlet.isEnabled = true
} else if (question == totalQuestions) {
lastOutlet.isEnabled = true
nextOutlet.isEnabled = false
} else {
lastOutlet.isEnabled = true
nextOutlet.isEnabled = true
}
}
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
}
}
}

最新更新