Skip to content

Commit

Permalink
adding closures for callbacks on segment selection
Browse files Browse the repository at this point in the history
  • Loading branch information
wasim009 committed Nov 1, 2019
1 parent 7df774a commit c462abe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 8 additions & 3 deletions WMSegmentControl/SegmentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -38,7 +43,7 @@ class SegmentViewController: UITableViewController {
print("Third item")
default:
print("default item")
}
}*/
}

}
Expand Down
4 changes: 2 additions & 2 deletions WMSegmentControl/Source/WMSegment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -246,7 +246,7 @@ open class WMSegment: UIControl {
btn.setTitleColor(selectorTextColor, for: .normal)
}
}

onValueChanged?(selectedSegmentIndex)
sendActions(for: .valueChanged)
}
//MARK: set Selected Index
Expand Down

0 comments on commit c462abe

Please sign in to comment.