求助:使用ScrollUISegmentController滚动时修改文本名称的问题
Hey there! Let's work through the text update problem you're facing when scrolling through segments with ScrollUISegmentController. From the code snippet you shared, I see you've already set up the delegate protocol, so let's dive into the key fixes and checks you need to make:
Implement the correct delegate method for segment selection
Most custom segment controllers like ScrollUISegmentController fire a delegate event when a segment is selected (or scrolled to). You'll need to add this method to yourFirstViewControllerto update your labels when the segment changes. Here's an example of what that might look like:func scrollUISegmentController(_ segmentController: ScrollUISegmentController, didSelectSegmentAt index: Int) { // Update your labels based on the selected segment index switch index { case 0: Label1.text = "Home Segment Text" Label2.text = "Secondary Home Text" Label3.text = "Tertiary Home Text" case 1: Label1.text = "Profile Segment Text" Label2.text = "Secondary Profile Text" Label3.text = "Tertiary Profile Text" case 2: Label1.text = "Settings Segment Text" Label2.text = "Secondary Settings Text" Label3.text = "Tertiary Settings Text" default: break } }Note: Double-check the exact method name in the ScrollUISegmentController's documentation—some variants might use a slightly different signature, but this is the standard pattern.
Verify your viewDidLoad setup
Make sure you're assigning the delegate and initializing your segment correctly inviewDidLoad. Missing these steps can prevent the delegate events from firing at all:override func viewDidLoad() { super.viewDidLoad() // Assign the delegate to self segment.delegate = self // Initialize your segment titles (if you haven't already) segment.segmentTitles = ["Home", "Profile", "Settings"] }Check your Label IBOutlets
It's easy to overlook a broken IBOutlet connection! Head back to your Storyboard/XIB and confirm thatLabel1,Label2, andLabel3are properly linked to their corresponding UI elements. If the connection is broken (you'll see an empty circle next to the IBOutlet in code), re-link them to fix the issue.For real-time updates during scrolling (not just on selection)
If you want to update text while the user is scrolling between segments (not just when they land on one), look for a delegate method that tracks scroll progress. Some implementations have a method like:func scrollUISegmentController(_ segmentController: ScrollUISegmentController, didScrollToProgress progress: CGFloat, fromSegment startIndex: Int, toSegment endIndex: Int) { // Use the progress value (0.0 to 1.0) to interpolate text changes // Example: Fade between two text values as the user scrolls let startText = "Start Text" let endText = "End Text" Label1.text = progress < 0.5 ? startText : endText }
内容的提问来源于stack exchange,提问作者tester beta

