@@ -11,6 +11,9 @@ import UIKit
11
11
public class Navigator {
12
12
13
13
public weak var viewController : UIViewController ?
14
+ var navigationController : UINavigationController ? {
15
+ viewController? . navigationController
16
+ }
14
17
15
18
public func set( _ viewController: UIViewController ? ) {
16
19
self . viewController = viewController
@@ -26,52 +29,51 @@ public extension Navigator {
26
29
27
30
func present( _ destination: UIViewController , style: UIModalPresentationStyle = . fullScreen, animated: Bool = true ) {
28
31
destination. modalPresentationStyle = style
29
- self . viewController ? . navigationController? . topViewController? . present ( destination, animated: animated)
32
+ navigationController? . topViewController? . present ( destination, animated: animated)
30
33
}
31
34
}
32
35
33
36
// Dismiss, Dismiss
34
37
public extension Navigator {
35
38
36
39
func popToRootViewController( _ animated: Bool = true ) {
37
- viewController ? . navigationController? . popToRootViewController ( animated: animated)
40
+ navigationController? . popToRootViewController ( animated: animated)
38
41
}
39
42
40
43
func pop( animated: Bool = true ) {
41
- self . viewController ? . navigationController? . popViewController ( animated: animated)
44
+ navigationController? . popViewController ( animated: animated)
42
45
}
43
46
44
47
func pop( to controller: AnyClass , animated: Bool = true ) {
45
48
46
- let target = self . viewController? . navigationController? . viewControllers
49
+ let target = self . navigationController?
50
+ . viewControllers
47
51
. filter { $0. isMember ( of: controller) }
48
52
. first
49
53
50
54
guard let target = target else { return }
51
- self . viewController ? . navigationController ? . popToViewController ( target, animated: animated)
55
+ popToViewController ( target, animated: animated)
52
56
}
53
57
54
58
func pop( to identifier: String , animated: Bool = true ) {
55
- let target = self . viewController? . navigationController? . viewControllers
59
+ let target = self . navigationController?
60
+ . viewControllers
56
61
. filter { $0. identifier == identifier }
57
62
. first
58
63
59
64
guard let target = target else { return }
60
- self . viewController ? . navigationController ? . popToViewController ( target, animated: animated)
65
+ popToViewController ( target, animated: animated)
61
66
}
62
67
63
68
func pop( toOneOf controllers: AnyClass ... , animated: Bool = true ) {
64
69
65
- guard let viewControllers = self
66
- . viewController?
67
- . navigationController?
68
- . viewControllers
70
+ guard let viewControllers = navigationController? . viewControllers
69
71
else { return }
70
72
71
73
for controller in viewControllers {
72
74
let shouldPop = controllers. contains ( where: { controller. isMember ( of: $0) } )
73
75
guard shouldPop else { continue }
74
- self . viewController ? . navigationController ? . popToViewController ( controller, animated: animated)
76
+ popToViewController ( controller, animated: animated)
75
77
}
76
78
}
77
79
@@ -88,15 +90,23 @@ public extension Navigator {
88
90
. first
89
91
90
92
guard let target = target else { return }
91
- self . viewController? . navigationController? . popToViewController ( target, animated: animated)
93
+ popToViewController ( target, animated: animated)
94
+ }
95
+
96
+ func popToViewController( _ controller: UIViewController , animated: Bool = true ) {
97
+ navigationController? . popToViewController ( controller, animated: animated)
92
98
}
93
99
100
+
94
101
func dismiss( animated: Bool = true ) {
95
- self . viewController? . dismiss ( animated: animated)
102
+ viewController? . dismiss ( animated: animated)
96
103
}
97
104
98
105
func removeFromParentAndSuperView( ) {
99
- self . viewController? . navigationController? . topViewController? . removeFromParentAndSuperView ( )
106
+ viewController?
107
+ . navigationController?
108
+ . topViewController?
109
+ . removeFromParentAndSuperView ( )
100
110
}
101
111
}
102
112
#endif
0 commit comments