Skip to content

Commit ccabfaf

Browse files
committed
Get index tab selected
1 parent 959e589 commit ccabfaf

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Demo/TabPageViewControllerDemo/ViewController.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class ViewController: UIViewController {
3131
option.tabWidth = view.frame.width / CGFloat(tc.tabItems.count)
3232
option.hidesTopViewOnSwipeType = .all
3333
tc.option = option
34+
tc.indexSelected = { (index) in
35+
print("--------------------------------")
36+
print(index)
37+
}
3438
navigationController?.pushViewController(tc, animated: true)
3539
}
3640

@@ -54,6 +58,10 @@ class ViewController: UIViewController {
5458
option.currentColor = UIColor(red: 246/255, green: 175/255, blue: 32/255, alpha: 1.0)
5559
option.tabMargin = 30.0
5660
tc.option = option
61+
tc.indexSelected = { (index) in
62+
print("--------------------------------")
63+
print(index)
64+
}
5765
navigationController?.pushViewController(tc, animated: true)
5866
}
5967
}

Sources/TabPageViewController.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ open class TabPageViewController: UIPageViewController {
1212
open var isInfinity: Bool = false
1313
open var option: TabPageOption = TabPageOption()
1414
open var tabItems: [(viewController: UIViewController, title: String)] = []
15-
15+
open var indexSelected: ((Int) -> Void)?
16+
1617
var currentIndex: Int? {
1718
guard let viewController = viewControllers?.first else {
1819
return nil
@@ -179,6 +180,7 @@ extension TabPageViewController {
179180
tabView.updateCurrentIndex(beforeIndex, shouldScroll: true)
180181

181182
tabView.pageItemPressedBlock = { [weak self] (index: Int, direction: UIPageViewControllerNavigationDirection) in
183+
self?.indexSelected?(index)
182184
self?.displayControllerWithIndex(index, direction: direction, animated: true)
183185
}
184186

@@ -296,7 +298,7 @@ extension TabPageViewController: UIPageViewControllerDataSource {
296298
guard var index = tabItems.map({$0.viewController}).index(of: viewController) else {
297299
return nil
298300
}
299-
301+
self.indexSelected?(index)
300302
if isAfter {
301303
index += 1
302304
} else {

0 commit comments

Comments
 (0)