0

I have set an IBOutlet function for a segmented control. It will display a value only when a user changes the value, but I am wondering how would I be able to display the default value if the user does not select a different option.

For example, a segmented control with [First, Second, Third] would print Second when the user selects Second, but would not print First until the user selects something else and THEN First.

Very simple answer I'm sure, but I am new to Swift so please excuse me.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Kevin
  • 1,189
  • 2
  • 17
  • 44

2 Answers2

3

In your UIViewController viewDidLoad method you should be able to display the default value of the segmented control:

override func viewDidLoad() {
   super.viewDidLoad()
   myLabel?.text = segment.titleForSegmentAtIndex(segment.selectedSegmentIndex)
}
mtaweel
  • 507
  • 4
  • 11
2

I think, you are looking for:

segmentedControl.titleForSegmentAtIndex(segmentedControl.selectedSegmentIndex)

which returns the title of currently selected segment.

I created a very simple test project demonstrating how that works.

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119