【问题标题】:How access properties of buttons from outside an outlet - Swift如何从插座外部访问按钮的属性 - Swift
【发布时间】: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
        }
    }

}

【问题讨论】:

  • 点击下一个和上一个按钮时,检查问题的计数是否等于最后禁用下一个,如果是起始索引则禁用上一个。

标签: swift uibutton iboutlet


【解决方案1】:

取出按钮使用它。像这样:-

class QuestionController: UIViewController {

    @IBOutlet weak var questionTextView: UITextView!
    @IBOutlet weak var btnNext: UIButton!
}

【讨论】:

  • 我尝试通过控制从按钮单击到类来尝试创建一个 var 出口,但它不会让我这样做,我认为这可能是因为我已经有一个按钮的 func 出口(保持按钮的动作)?
  • 按钮的实例在action和outlet上都是一样的。
【解决方案2】:

感谢我最终能够创建出口。然后我用下面的 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
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    相关资源
    最近更新 更多