简体   繁体   中英

How do I use UISegmentedControl to change the labels in Swift?

I am a beginner to Swift so please bear with me.

Here is the documentation for using UISegmentedControl:

Code.

Every single time I try using that code I get compilation code. I am currently trying to get the code to change segmentation 3 (custom) to whatever the user inputs (so a string).

I have tried:

func setTitle(_ title: "Hello"?,
forSegmentAtIndex segment: 3) 

and

func setTitle(_ title: Hello?,
forSegmentAtIndex segment: 3)

and

func setTitle(_ title: "Hello"?,
forSegmentAtIndex segment: Int 3)

No idea how to correctly do this, please help!

Also, here's a link to the app i'm trying to make: http://i.imgur.com/CWoBkHk.gif . I just really want the custom button to reflect what the customized tip % is.

Try like this:-

Assuming you have outlet :-

@IBOutlet weak var segmented :UISegmentedControl?

//Now for changing the title:-
segmented?.setTitle("test1", forSegmentAtIndex: 0)
segmented?.setTitle("test2", forSegmentAtIndex: 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM