Question

I have a couple of custom NSBox subclasses called OuterBox and InnerBox. I've set up my view in a XIB and arranged the hierarchy like this:

OuterBox : NSBox
    NSButton
    NSBox
    InnerBox : NSBox
    ...and some other views

The idea is that when the NSButton gets pressed, in an IBAction method, I want to get the superview of the button and then, from that, get the InnerBox that is in the same OuterBox as the button.

To do this, I loop through the OuterBox's subviews and check their type:

for (NSObject *subview in [outerBox subviews]) {
    // this never evaluates to true...
    if ([subview isKindOfClass:[InnerBox class]]) {
        // ...
    }
}

The problem is that my if statement never hits. subview shows up in the debugger as an NSView. According to the documentation, isKindOfClass:

returns YES if the receiver is an instance of aClass or an instance of any class that inherits from aClass, otherwise NO.

I understand why it's returning NO: because InnerBox is a type of NSView but not vice versa. But I don't know why subview is a UIView when it should be an InnerBox.

I have imported InnerBox.h and made sure that the InnerBox really is an InnerBox in the XIB. I don't know what could be causing its type to get changed, or be reported incorrectly...

Was it helpful?

Solution

As far as I can tell, isKindOfClass: should return YES when it encounters your InnerBox. Perhaps the views aren't nested the way you intended. Two things to check:

  1. Is the superview of the NSButton truly the OuterBox? (You could NSLog the superview of the sender in your button's action method.)
  2. Is the InnerBox truly a subview of the OuterBox? (Perhaps set up an IBOutlet to the InnerBox and NSLog its superview.)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top