diff --git a/WMSegmentControl/SegmentViewController.swift b/WMSegmentControl/SegmentViewController.swift index 09c5299..d4264c7 100644 --- a/WMSegmentControl/SegmentViewController.swift +++ b/WMSegmentControl/SegmentViewController.swift @@ -25,11 +25,16 @@ class SegmentViewController: UITableViewController { sgTextOnlyBar.selectorType = .bottomBar sgTextOnlyBar.SelectedFont = UIFont(name: "ChalkboardSE-Bold", size: 15)! sgTextOnlyBar.normalFont = UIFont(name: "ChalkboardSE-Regular", size: 15)! - // Do any additional setup after loading the view. + + //Using callbacks + sgTextOnly.onValueChanged = { index in + print("I have selected index \(index) from WMSegment!") + } } @IBAction func segmentValueChange(_ sender: WMSegment) { - switch sender.selectedSegmentIndex { + print("selected index = \(sender.selectedSegmentIndex)") + /*switch sender.selectedSegmentIndex { case 0: print("first item") case 1: @@ -38,7 +43,7 @@ class SegmentViewController: UITableViewController { print("Third item") default: print("default item") - } + }*/ } } diff --git a/WMSegmentControl/Source/WMSegment.swift b/WMSegmentControl/Source/WMSegment.swift index 00a0cac..d76836b 100644 --- a/WMSegmentControl/Source/WMSegment.swift +++ b/WMSegmentControl/Source/WMSegment.swift @@ -9,7 +9,7 @@ import UIKit @IBDesignable open class WMSegment: UIControl { - + var onValueChanged: ((_ index: Int)->())? var buttons = [UIButton]() var selector: UIView! public var selectedSegmentIndex: Int = 0 @@ -246,7 +246,7 @@ open class WMSegment: UIControl { btn.setTitleColor(selectorTextColor, for: .normal) } } - + onValueChanged?(selectedSegmentIndex) sendActions(for: .valueChanged) } //MARK: set Selected Index