-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(uiview): Add extension to find parent view controller (#21)
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// UIView+Parent.swift | ||
// DSKit | ||
// | ||
// Created by David Chavez on 13/1/20. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UIView { | ||
var parentViewController: UIViewController? { | ||
var parentResponder: UIResponder? = self | ||
while parentResponder != nil { | ||
parentResponder = parentResponder?.next | ||
if let viewController = parentResponder as? UIViewController { | ||
return viewController | ||
} | ||
} | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// UIView+Parent.swift | ||
// DSKit | ||
// | ||
// Created by David Chavez on 1/2/24. | ||
// | ||
|
||
import XCTest | ||
import UIKit | ||
@testable import DSKit | ||
|
||
class ParentViewControllerTests: XCTestCase { | ||
func testParentViewControllerSimpleCase() { | ||
let parent = UIViewController() | ||
let child = UIView() | ||
parent.view.addSubview(child) | ||
|
||
XCTAssert(child.parentViewController === parent) | ||
} | ||
|
||
func testParentViewControllerNestedCase() { | ||
let parent = UIViewController() | ||
let child = UIView() | ||
let grandChild = UIView() | ||
|
||
parent.view.addSubview(child) | ||
child.addSubview(grandChild) | ||
|
||
XCTAssert(grandChild.parentViewController === parent) | ||
} | ||
} |