开发者

Problem Implementing UIView Category

For whatever reason, the following code isn't working:

#import <Foundation/Foundation.h>

@interface UIView (FindUIViewControllerParent)

- (UIViewController *) firstAvailableUIViewController;
- (id) traverseResponderChainForUIViewController;

@end


#import "UIView+FindUIViewControllerParent.h"

@implementation UIView (FindUIViewControllerParent)

- (UIViewController *) firstAvailableUIViewController 
{
    return (UIViewController *)[self traverseResponderChainForUIViewController];
}

- (id) traverseResponderChainForUIViewController 
{
    id nextResponder = [self nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) 
    {
        return nextResponder;
    } 
    else if ([nextResponder isKindOfClass:[UIView class]]) 
    {
        return [nextResponder traverseResponderChainForUIViewController];
    } 
    else 
    {
        return nil;
    }
}

@end

parentViewController = [self firstAvailableUIViewController];

The firstResponder is coming back as nil, instead of being the parent view controller (when I did it without the category it worked out fine). I've read some other threads that say to make some adjustments to Other Linking Options in project settings, and I've tried these an开发者_开发百科d it still doesn't work. Can anyone see what I'm doing wrong here? (this code is copied nearly exactly from another thread that got a ton of up votes).


I found my problem. I was looking for and storing the nextResponder in the init method my view, before the view had even been attached to a view controller. That's why it was correctly showing up null. I used my category method in the button action method instead and then it worked fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜