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

时间:2017-05-15 10:48:20

标签: swift uibutton iboutlet

我有一个场景,可以显示数组中的问题。故事板包括两个按钮以转到下一个/上一个问题。我有一个方法,要求班级持有问题(甲板)当前章节,当前问题和总问题的数量。这用于更新标题。

我希望能够使用我的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
        }
    }

}

2 个答案:

答案 0 :(得分:0)

按下按钮的出口使用它。就像那样: -

class QuestionController: UIViewController {

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

答案 1 :(得分:0)

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

}