【问题标题】:Display array info in text box and button in swift在文本框和按钮中快速显示数组信息
【发布时间】:2020-10-27 03:54:00
【问题描述】:

当我运行应用程序时,如何在 storyLabel 文本、choice1Button 和choice2Button 中显示我的故事数组的第一行中的信息?

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var storyLabel: UILabel!
    @IBOutlet weak var choice1Button: UIButton!
    @IBOutlet weak var choice2Button: UIButton!
    
    let stories = [

        Story(title: "You see a fork in the road", choice1: "Take a left", choice2: "Take a right"),
        Story(title: "You see a tiger", choice1: "Shout for help", choice2: "Play dead"),
        Story(title: "You find a treasure chest", choice1: "Open it", choice2: "Check for traps")
    ]
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        updateUI()

        
    }
    
    @objc func updateUI() {
        storyLabel.text = [1][0]
        choice1Button.setTitle(choice1, for: .normal)
        choice2Button.setTitle(Story[2], for: .normal)
    }
    
    @IBAction func choiceMade(_ sender: UIButton) {
    }
    
}

【问题讨论】:

    标签: arrays swift struct


    【解决方案1】:

    在这个问题上睡觉后,我设法解决了它;

    导入 UIKit

    类视图控制器:UIViewController {

    @IBOutlet weak var storyLabel: UILabel!
    @IBOutlet weak var choice1Button: UIButton!
    @IBOutlet weak var choice2Button: UIButton!
    
    var storyNumber = 0
    
    let stories = [
    
        Story(title: "You see a fork in the road", choice1: "Take a left", choice2: "Take a right"),
        Story(title: "You see a tiger", choice1: "Shout for help", choice2: "Play dead"),
        Story(title: "You find a treasure chest", choice1: "Open it", choice2: "Check for traps")
    ]
    
    func getStoryTitle() -> String {
        return stories[storyNumber].title
    }
    
    func getChoice1() -> String{
        return stories[storyNumber].choice1
    }
    
    func getChoice2() -> String {
        return stories[storyNumber].choice2
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        updateUI()
    
        
    }
    
    func updateUI() {
        storyLabel.text = getStoryTitle()
        choice1Button.setTitle(getChoice1(), for: .normal)
        choice2Button.setTitle(getChoice2(), for: .normal)
    }
    
    @IBAction func choiceMade(_ sender: UIButton) {
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-25
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 2021-08-06
      • 1970-01-01
      相关资源
      最近更新 更多