A small category on NSObject
that adds support for recursive lookup using valueForKey:
:
NSArray *superviewChain = [view recursiveValueForKey:@"superview"];
// Recursively gets the superview property until it is nil
Like the traditional valueForKey:
method, when used on collection classes (NSArray
, NSSet
, and NSOrderedSet
) it applies the method to each of the objects inside the collection.
NSArray *allSubviews = [view recursiveValueForKey:@"subviews"];
// Returns all the subviews of a view, and all their subviews, and all their subviews, etc. in a flat array
When a UITableViewCell
is highlighted, it attempts to highlight all of its subviews as well. This behavior looks nice for labels, but typically looks poor when applied to buttons. The code below overrides the default behavior to not highlight the buttons.
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if (highlighted) {
[super setHighlighted:highlighted animated:animated];
NSArray *recursiveAllSubviews = [self recursiveValueForKey:@"subviews"];
for (UIView *subview in recursiveAllSubviews) {
if ([subview isKindOfClass:[UIButton class]]) {
[subview setValue:[NSNumber numberWithBool:NO] forKey:@"highlighted"];
}
}
} else {
[super setHighlighted:highlighted animated:animated];
}
}
(Note: You would need the same code in setSelected:animated:
in actual usage).
###Cocoapods
Cocoapods is the recommended installation method:
- Install CocoaPods
[sudo] gem install cocoapods
pod setup
- Make a file with no extension named
Podfile
touch Podfile
- Configure the Podfile
platform :ios
pod 'MTRecursiveKVC'
###Drag-n-Drop
Simply drag in the 4 categories in the project.
Full API documentation is available on CocoaDocs.
If you install MTRecursiveKVC using Cocoapods, Cocoapods will automatically add the documentation to Xcode for you if you have Appledoc installed.
You can also install documentation locally using Appledoc. I use the following command to generate documentation for it:
appledoc --project-name MTRecursiveKVC --project-company "Maximilian Tagher" --company-id com.tagher.rkvc --ignore "*.m" --no-repeat-first-par --docset-platform-family iphoneos --output ~/help .
I consider this project complete -- it's unit-tested, documented, and I use it in production. The limited scope of this project means you shouldn't be too concerned about the lack of pull requests.
If you find any bugs, you can start an issue. If you use MTRecursiveKVC
, I'd be happy to hear about it on Twitter.
MTRecursiveKVC is tested using the Kiwi BDD framework. You can use Cocoapods to set it up:
-
Install Cocoapods: Follow the instructions here to install: https://cocoapods.org/ For a good introduction, check out this screencast: https://nsscreencast.com/episodes/28-creating-a-cocoapod
-
Run pod install
-
Open the .xcworkspace file.