You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift技术问询:结构体内部属性调用方法与按钮点击返回指定Int值实现

Hey there! Let's break down your two Swift questions step by step.

问题一:Swift中如何调用结构体(struct)内部的属性?

Structs in Swift are value types, and accessing their properties is straightforward once you have an instance of the struct. Here's how it works:

  1. First, define your struct (like the Story struct you're using in your code):

    struct Story {
        let title: String
        let choice1: String
        let choice1Destination: Int
        let choice2: String
        let choice2Destination: Int
    }
    
  2. Create an instance of the struct:

    let sampleStory = Story(title: "Sample Title", choice1: "Choice A", choice1Destination: 1, choice2: "Choice B", choice2Destination: 2)
    
  3. Use dot syntax (.) to access its properties:

    // Access the title property
    print(sampleStory.title) // Output: "Sample Title"
    
    // Access the destination value
    let nextStoryIndex = sampleStory.choice1Destination // nextStoryIndex will be 1
    

If your struct has static properties (properties shared across all instances), you can access them directly using the struct name without creating an instance:

struct AppConstants {
    static let maxStoryCount = 6
}

// Access static property
print(AppConstants.maxStoryCount) // Output: 6
问题二:UIViewController中点击按钮返回对应Destination值的实现

Looking at your code, there are a couple of tweaks needed to make the button taps return the correct choice1Destination or choice2Destination values. Let's fix this:

Step 1: Add a variable to track the current story index

First, add a property to your ViewController class to keep track of which story we're currently displaying:

class ViewController: UIViewController { 
    @IBOutlet weak var storyLabel: UILabel! 
    @IBOutlet weak var choice1Button: UIButton! 
    @IBOutlet weak var choice2Button: UIButton!

    // Add this line to track the current story
    var currentStoryIndex = 0

    // ... rest of your code ...
}

Step 2: Fix the button tap handler

Your original code tries to compare the button's title to the button's name, which won't work (the button's title is the text displayed on it, like "I'll hop in. Thanks for the help!"). Instead, we'll check which button instance was tapped, then grab the corresponding destination value:

@IBAction func pressButton(_ sender: UIButton) {
    // Get the current story using our tracked index
    let currentStory = story[currentStoryIndex]
    
    var destination: Int
    if sender === choice1Button {
        // Get choice1's destination when choice1Button is tapped
        destination = currentStory.choice1Destination
    } else if sender === choice2Button {
        // Get choice2's destination when choice2Button is tapped
        destination = currentStory.choice2Destination
    } else {
        // Fallback if an unexpected button is tapped
        destination = 0
        return
    }

    // Now you can use this destination value however you need—for example, update the story
    currentStoryIndex = destination
    updateUI() // Refresh the UI to show the new story
}

Step 3: Make sure your updateUI() method is properly implemented

This method should update the label and buttons to match the current story:

func updateUI() {
    let currentStory = story[currentStoryIndex]
    storyLabel.text = currentStory.title
    choice1Button.setTitle(currentStory.choice1, for: .normal)
    choice2Button.setTitle(currentStory.choice2, for: .normal)
}

With these changes, tapping choice1Button will grab the choice1Destination of the current story, and tapping choice2Button will grab choice2Destination—then we use that value to switch to the next story and update the UI.


内容的提问来源于stack exchange,提问作者Natalie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 07:32:42