suckypizza
suckypizza

Reputation: 39

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.

Upvotes: 1

Views: 3088

Answers (1)

Hussain Shabbir
Hussain Shabbir

Reputation: 15015

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)

Upvotes: 5

Related Questions