Skip to content

Commit

Permalink
Add methods to UIView and UIWindow to find the first responder
Browse files Browse the repository at this point in the history
  • Loading branch information
danielctull committed May 12, 2011
1 parent 3afa0f7 commit 5799eb0
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Categories/UIView+DCTFirstResponder.h
@@ -0,0 +1,41 @@
/*
UIView+DCTFirstResponder.h
DCTUIKit
Created by Daniel Tull on 12.5.2011.
Copyright (c) 2011 Daniel Tull. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import <UIKit/UIKit.h>

@interface UIView (DCTFirstResponder)
- (UIResponder *)dct_findFirstResponder;
@end
53 changes: 53 additions & 0 deletions Categories/UIView+DCTFirstResponder.m
@@ -0,0 +1,53 @@
/*
UIView+DCTFirstResponder.m
DCTUIKit
Created by Daniel Tull on 12.5.2011.
Copyright (c) 2011 Daniel Tull. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import "UIView+DCTFirstResponder.h"

@implementation UIView (DCTFirstResponder)

- (UIResponder *)dct_findFirstResponder {

if ([self isFirstResponder]) return self;

for (UIView *v in self.subviews) {
UIResponder *r = [v dct_findFirstResponder];
if ((r)) return r;
}

return nil;
}

@end
41 changes: 41 additions & 0 deletions Categories/UIWindow+DCTFirstResponder.h
@@ -0,0 +1,41 @@
/*
UIWindow+DCTFirstResponder.h
DCTUIKit
Created by Daniel Tull on 12.5.2011.
Copyright (c) 2011 Daniel Tull. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import <UIKit/UIKit.h>

@interface UIWindow (DCTFirstResponder)
@property (nonatomic, readonly) UIResponder *dct_firstResponder;
@end
46 changes: 46 additions & 0 deletions Categories/UIWindow+DCTFirstResponder.m
@@ -0,0 +1,46 @@
/*
UIWindow+DCTFirstResponder.m
DCTUIKit
Created by Daniel Tull on 12.5.2011.
Copyright (c) 2011 Daniel Tull. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import "UIWindow+DCTFirstResponder.h"
#import "UIView+DCTFirstResponder.h"

@implementation UIWindow (DCTFirstResponder)

- (UIResponder *)dct_firstResponder {
return [self dct_findFirstResponder];
}

@end
20 changes: 20 additions & 0 deletions DCTUIKit.xcodeproj/project.pbxproj
Expand Up @@ -71,6 +71,12 @@
24B8EB6E13491326007A7E2B /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 24B8EB6D13491326007A7E2B /* main.m */; };
24B8EB7413491326007A7E2B /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 24B8EB7213491326007A7E2B /* MainWindow.xib */; };
24B8EB7713491326007A7E2B /* DCTUIKitApp.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 24B8EB7513491326007A7E2B /* DCTUIKitApp.xcdatamodeld */; };
24F54A74137C8711000AC87E /* UIWindow+DCTFirstResponder.h in Headers */ = {isa = PBXBuildFile; fileRef = 24F54A72137C8711000AC87E /* UIWindow+DCTFirstResponder.h */; };
24F54A75137C8711000AC87E /* UIWindow+DCTFirstResponder.m in Sources */ = {isa = PBXBuildFile; fileRef = 24F54A73137C8711000AC87E /* UIWindow+DCTFirstResponder.m */; };
24F54A76137C8711000AC87E /* UIWindow+DCTFirstResponder.m in Sources */ = {isa = PBXBuildFile; fileRef = 24F54A73137C8711000AC87E /* UIWindow+DCTFirstResponder.m */; };
24F54A77137C8711000AC87E /* UIWindow+DCTFirstResponder.m in Sources */ = {isa = PBXBuildFile; fileRef = 24F54A73137C8711000AC87E /* UIWindow+DCTFirstResponder.m */; };
24F54A7B137C88DA000AC87E /* UIView+DCTFirstResponder.h in Headers */ = {isa = PBXBuildFile; fileRef = 24F54A79137C88DA000AC87E /* UIView+DCTFirstResponder.h */; };
24F54A7C137C88DA000AC87E /* UIView+DCTFirstResponder.m in Sources */ = {isa = PBXBuildFile; fileRef = 24F54A7A137C88DA000AC87E /* UIView+DCTFirstResponder.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -152,6 +158,10 @@
24B8EB6D13491326007A7E2B /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
24B8EB7313491326007A7E2B /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/MainWindow.xib; sourceTree = "<group>"; };
24B8EB7613491326007A7E2B /* DCTUIKitApp.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = DCTUIKitApp.xcdatamodel; sourceTree = "<group>"; };
24F54A72137C8711000AC87E /* UIWindow+DCTFirstResponder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIWindow+DCTFirstResponder.h"; sourceTree = "<group>"; };
24F54A73137C8711000AC87E /* UIWindow+DCTFirstResponder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIWindow+DCTFirstResponder.m"; sourceTree = "<group>"; };
24F54A79137C88DA000AC87E /* UIView+DCTFirstResponder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+DCTFirstResponder.h"; sourceTree = "<group>"; };
24F54A7A137C88DA000AC87E /* UIView+DCTFirstResponder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+DCTFirstResponder.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -212,6 +222,10 @@
241A222A134913D200574B99 /* UIView+DCTSubviewExtensions.m */,
241A222B134913D200574B99 /* UIViewController+DCTExtras.h */,
241A222C134913D200574B99 /* UIViewController+DCTExtras.m */,
24F54A72137C8711000AC87E /* UIWindow+DCTFirstResponder.h */,
24F54A73137C8711000AC87E /* UIWindow+DCTFirstResponder.m */,
24F54A79137C88DA000AC87E /* UIView+DCTFirstResponder.h */,
24F54A7A137C88DA000AC87E /* UIView+DCTFirstResponder.m */,
);
path = Categories;
sourceTree = "<group>";
Expand Down Expand Up @@ -413,6 +427,8 @@
241A226F134913D200574B99 /* DCTContentViewController.h in Headers */,
241A2271134913D200574B99 /* DCTTableViewController.h in Headers */,
241A2273134913D200574B99 /* DCTViewController.h in Headers */,
24F54A74137C8711000AC87E /* UIWindow+DCTFirstResponder.h in Headers */,
24F54A7B137C88DA000AC87E /* UIView+DCTFirstResponder.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -564,6 +580,8 @@
241A2270134913D200574B99 /* DCTContentViewController.m in Sources */,
241A2272134913D200574B99 /* DCTTableViewController.m in Sources */,
241A2274134913D200574B99 /* DCTViewController.m in Sources */,
24F54A75137C8711000AC87E /* UIWindow+DCTFirstResponder.m in Sources */,
24F54A7C137C88DA000AC87E /* UIView+DCTFirstResponder.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -572,6 +590,7 @@
buildActionMask = 2147483647;
files = (
24B8EB4C134912BF007A7E2B /* DCTUIKitTests.m in Sources */,
24F54A76137C8711000AC87E /* UIWindow+DCTFirstResponder.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -583,6 +602,7 @@
24B8EB7713491326007A7E2B /* DCTUIKitApp.xcdatamodeld in Sources */,
241A22171349137B00574B99 /* DCTUIKitAppDelegate.m in Sources */,
241A22B6134913E800574B99 /* TestViewController.m in Sources */,
24F54A77137C8711000AC87E /* UIWindow+DCTFirstResponder.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 5799eb0

Please sign in to comment.