From 89606de02cf0b35e8558bbb0cdef9eb006293c3d Mon Sep 17 00:00:00 2001 From: Nate Walczak Date: Tue, 26 May 2015 18:03:23 -0400 Subject: [PATCH] add top presented view controller category --- DLRUIKit.podspec | 2 +- DLRUIKit.xcodeproj/project.pbxproj | 12 +++++ ...iewController+DLRPresentedViewController.h | 16 +++++++ ...iewController+DLRPresentedViewController.m | 23 ++++++++++ ...ntroller+DLRPresentedViewControllerTests.m | 44 +++++++++++++++++++ README.md | 7 +++ 6 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 DLRUIKit/source/UIViewController+DLRPresentedViewController.h create mode 100644 DLRUIKit/source/UIViewController+DLRPresentedViewController.m create mode 100644 DLRUIKitTests/source/UIViewController+DLRPresentedViewControllerTests.m diff --git a/DLRUIKit.podspec b/DLRUIKit.podspec index 6dfcdce..e4427a9 100644 --- a/DLRUIKit.podspec +++ b/DLRUIKit.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "DLRUIKit" - s.version = "1.3.0" + s.version = "1.4.0" s.summary = "A collection of UIKit categories." s.homepage = "https://github.com/detroit-labs/dlr-uikit-ios" s.license = 'MIT' diff --git a/DLRUIKit.xcodeproj/project.pbxproj b/DLRUIKit.xcodeproj/project.pbxproj index 1f492d3..5c3972e 100644 --- a/DLRUIKit.xcodeproj/project.pbxproj +++ b/DLRUIKit.xcodeproj/project.pbxproj @@ -25,6 +25,9 @@ 433C59FD1AC335BE0014EFB8 /* DLRLayoutConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = 433C59FB1AC335BE0014EFB8 /* DLRLayoutConstraint.m */; }; 433C5A001AC336B10014EFB8 /* DLRLayoutConstraintTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 433C59FF1AC336B10014EFB8 /* DLRLayoutConstraintTests.m */; }; 433C5A041AC338250014EFB8 /* LayoutConstraintViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 433C5A031AC338250014EFB8 /* LayoutConstraintViewController.m */; }; + 435C37501B151C98003A0910 /* UIViewController+DLRPresentedViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 435C374E1B151C98003A0910 /* UIViewController+DLRPresentedViewController.h */; }; + 435C37511B151C98003A0910 /* UIViewController+DLRPresentedViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 435C374F1B151C98003A0910 /* UIViewController+DLRPresentedViewController.m */; }; + 435C37531B151DA9003A0910 /* UIViewController+DLRPresentedViewControllerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 435C37521B151DA9003A0910 /* UIViewController+DLRPresentedViewControllerTests.m */; }; 4362BDC01A8ABA5900B3A89E /* UIDevice+DLR.h in Headers */ = {isa = PBXBuildFile; fileRef = 4362BDBE1A8ABA5900B3A89E /* UIDevice+DLR.h */; }; 4362BDC11A8ABA5900B3A89E /* UIDevice+DLR.m in Sources */ = {isa = PBXBuildFile; fileRef = 4362BDBF1A8ABA5900B3A89E /* UIDevice+DLR.m */; }; 4362BDC31A8AC0C400B3A89E /* UIDevice+DLRTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4362BDC21A8AC0C400B3A89E /* UIDevice+DLRTests.m */; }; @@ -115,6 +118,9 @@ 433C59FF1AC336B10014EFB8 /* DLRLayoutConstraintTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DLRLayoutConstraintTests.m; sourceTree = ""; }; 433C5A021AC338250014EFB8 /* LayoutConstraintViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LayoutConstraintViewController.h; sourceTree = ""; }; 433C5A031AC338250014EFB8 /* LayoutConstraintViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LayoutConstraintViewController.m; sourceTree = ""; }; + 435C374E1B151C98003A0910 /* UIViewController+DLRPresentedViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIViewController+DLRPresentedViewController.h"; sourceTree = ""; }; + 435C374F1B151C98003A0910 /* UIViewController+DLRPresentedViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+DLRPresentedViewController.m"; sourceTree = ""; }; + 435C37521B151DA9003A0910 /* UIViewController+DLRPresentedViewControllerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+DLRPresentedViewControllerTests.m"; sourceTree = ""; }; 4362BDBE1A8ABA5900B3A89E /* UIDevice+DLR.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIDevice+DLR.h"; sourceTree = ""; }; 4362BDBF1A8ABA5900B3A89E /* UIDevice+DLR.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIDevice+DLR.m"; sourceTree = ""; }; 4362BDC21A8AC0C400B3A89E /* UIDevice+DLRTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIDevice+DLRTests.m"; sourceTree = ""; }; @@ -408,6 +414,8 @@ 4388EE151AF11E9100CD2140 /* UIViewController+DLRNavigationController.m */, 439373621A8521A800A552D5 /* UIViewController+DLRParentViewController.h */, 439373631A8521A800A552D5 /* UIViewController+DLRParentViewController.m */, + 435C374E1B151C98003A0910 /* UIViewController+DLRPresentedViewController.h */, + 435C374F1B151C98003A0910 /* UIViewController+DLRPresentedViewController.m */, 439373641A8521A800A552D5 /* UIWebView+DLRViewport.h */, 439373651A8521A800A552D5 /* UIWebView+DLRViewport.m */, ); @@ -428,6 +436,7 @@ 439373791A85220F00A552D5 /* UIView+DLRRoundedCornersTests.m */, 4388EE181AF11F0200CD2140 /* UIViewController+DLRNavigationControllerTests.m */, 4393737A1A85220F00A552D5 /* UIViewController+DLRParentViewControllerTests.m */, + 435C37521B151DA9003A0910 /* UIViewController+DLRPresentedViewControllerTests.m */, 4393737B1A85220F00A552D5 /* UIWebView+DLRViewportTests.m */, ); name = "Category Tests"; @@ -465,6 +474,7 @@ 4362BDC01A8ABA5900B3A89E /* UIDevice+DLR.h in Headers */, 439373661A8521A800A552D5 /* UIApplication+DLRLocalNotifications.h in Headers */, 4393736C1A8521A800A552D5 /* UIView+DLRLayoutConstraints.h in Headers */, + 435C37501B151C98003A0910 /* UIViewController+DLRPresentedViewController.h in Headers */, 4393736E1A8521A800A552D5 /* UIView+DLRRoundedCorners.h in Headers */, 439373701A8521A800A552D5 /* UIViewController+DLRParentViewController.h in Headers */, 43B4C20D1A8C19C300680976 /* UIViewController+DLRStoryboardManager.h in Headers */, @@ -633,6 +643,7 @@ 4388EE111AF11DA400CD2140 /* UINavigationController+DLR.m in Sources */, 439373731A8521A800A552D5 /* UIWebView+DLRViewport.m in Sources */, 439373691A8521A800A552D5 /* UIColor+DLR.m in Sources */, + 435C37511B151C98003A0910 /* UIViewController+DLRPresentedViewController.m in Sources */, 439373671A8521A800A552D5 /* UIApplication+DLRLocalNotifications.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -652,6 +663,7 @@ 4393737D1A85220F00A552D5 /* UIColor+DLRTests.m in Sources */, 4393737C1A85220F00A552D5 /* UIApplication+DLRLocalNotificationsTests.m in Sources */, 439373821A85220F00A552D5 /* UIWebView+DLRViewportTests.m in Sources */, + 435C37531B151DA9003A0910 /* UIViewController+DLRPresentedViewControllerTests.m in Sources */, 433C5A001AC336B10014EFB8 /* DLRLayoutConstraintTests.m in Sources */, 439373811A85220F00A552D5 /* UIViewController+DLRParentViewControllerTests.m in Sources */, 439373801A85220F00A552D5 /* UIView+DLRRoundedCornersTests.m in Sources */, diff --git a/DLRUIKit/source/UIViewController+DLRPresentedViewController.h b/DLRUIKit/source/UIViewController+DLRPresentedViewController.h new file mode 100644 index 0000000..6a13de5 --- /dev/null +++ b/DLRUIKit/source/UIViewController+DLRPresentedViewController.h @@ -0,0 +1,16 @@ +// +// UIViewController+DLRPresentedViewController.h +// DLRUIKit +// +// Created by Nate Walczak on 5/26/15. +// Copyright (c) 2015 Detroit Labs, LLC. All rights reserved. +// + +#import + +@interface UIViewController (DLRPresentedViewController) + +/** Returns the top most presented view controller, which could be this view controller. */ +- (UIViewController *)dlr_topPresentedViewController; + +@end diff --git a/DLRUIKit/source/UIViewController+DLRPresentedViewController.m b/DLRUIKit/source/UIViewController+DLRPresentedViewController.m new file mode 100644 index 0000000..58c8b0a --- /dev/null +++ b/DLRUIKit/source/UIViewController+DLRPresentedViewController.m @@ -0,0 +1,23 @@ +// +// UIViewController+DLRPresentedViewController.m +// DLRUIKit +// +// Created by Nate Walczak on 5/26/15. +// Copyright (c) 2015 Detroit Labs, LLC. All rights reserved. +// + +#import "UIViewController+DLRPresentedViewController.h" + +@implementation UIViewController (DLRPresentedViewController) + +- (UIViewController *)dlr_topPresentedViewController { + UIViewController *topPresentedViewController = self; + + while (topPresentedViewController.presentedViewController) { + topPresentedViewController = topPresentedViewController.presentedViewController; + } + + return topPresentedViewController; +} + +@end diff --git a/DLRUIKitTests/source/UIViewController+DLRPresentedViewControllerTests.m b/DLRUIKitTests/source/UIViewController+DLRPresentedViewControllerTests.m new file mode 100644 index 0000000..4622d9e --- /dev/null +++ b/DLRUIKitTests/source/UIViewController+DLRPresentedViewControllerTests.m @@ -0,0 +1,44 @@ +// +// UIViewController+DLRPresentedViewControllerTests.m +// DLRUIKit +// +// Created by Nate Walczak on 5/26/15. +// Copyright (c) 2015 Detroit Labs, LLC. All rights reserved. +// + +#import + +#import "UIViewController+DLRPresentedViewController.h" + +@interface UIViewController_DLRPresentedViewControllerTests : XCTestCase + +@end + +@implementation UIViewController_DLRPresentedViewControllerTests + +#pragma mark - dlr_topPresentedViewController: + +- (void)testTopPresentedViewControllerWithModalViewController { + UIViewController *rootViewController = [[UIViewController alloc] init]; + + UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; + keyWindow.rootViewController = rootViewController; + + UIViewController *firstModalViewController = [[UIViewController alloc] init]; + [rootViewController presentViewController:firstModalViewController animated:NO completion:nil]; + + UIViewController *topPresentedViewController = [rootViewController dlr_topPresentedViewController]; + XCTAssertEqual(topPresentedViewController, firstModalViewController); +} + +- (void)testTopPresentedViewControllerWithoutModalViewController { + UIViewController *rootViewController = [[UIViewController alloc] init]; + + UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; + keyWindow.rootViewController = rootViewController; + + UIViewController *topPresentedViewController = [rootViewController dlr_topPresentedViewController]; + XCTAssertEqual(topPresentedViewController, rootViewController); +} + +@end diff --git a/README.md b/README.md index fbe16a7..7bd8c51 100644 --- a/README.md +++ b/README.md @@ -189,3 +189,10 @@ layoutConstraint.constant = -10; root view controller. */ - (UINavigationController *)dlr_embedInsideNavigationController; ``` + +### UIViewController+DLRPresentedViewController Category Examples + +```objc +/** Returns the top most presented view controller, which could be this view controller. */ +- (UIViewController *)dlr_topPresentedViewController; +```