Swift技术问询:结构体内部属性调用方法与按钮点击返回指定Int值实现
Hey there! Let's break down your two Swift questions step by step.
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:
First, define your struct (like the
Storystruct you're using in your code):struct Story { let title: String let choice1: String let choice1Destination: Int let choice2: String let choice2Destination: Int }Create an instance of the struct:
let sampleStory = Story(title: "Sample Title", choice1: "Choice A", choice1Destination: 1, choice2: "Choice B", choice2Destination: 2)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
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

