开发者

Preprocessor Directive in xib?

Despite searching all over the place, I can't find the answer to my question. So let's see how good y'all are. :)

I'm working on an app that uses an NSPopover which is only available in 10.7 Lion but I want the app to compile for 10.5 and higher. I'm using a preprocessor directive to wrap the related popover code which seems to do the trick... However, the last piece I'm still getting errors on is the .zib in Interface Builder. How do I go about cleaning up the errors shown in the Issues Navigator开发者_开发技巧 stating "Class Unavailable: NSPopover on Mac OS X versions prior to 10.7"?

#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
    #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
        @property (assign) IBOutlet NSPopover *popover;
    }
    #endif
#endif

The above works in xxx.h and xxx.m's, but how do I get around the .xib errors?

Despite the error (Red), it builds successfully. However am I wrong to expect the 10.7 features (popover) to work in 10.7 because they don't... What am I missing here?


You shouldn't use preprocessors for this but check for availability at runtime using NSClassFromString(). The preprocessor runs at compile time, thus it won't detect what system the app is being run on.

Create three nibs, one for each of 10.5, 10.6 and 10.7 and load the one you need (or do it in code), but pick which one at run time, not compile time, e.g.

MyVC *vc = nil;
if (NSClassFromString(@"NSPopover"))
{
    vc = [NSViewController initWithNibName:@"MyVC-Lion" bundle:nil];
}
else if (/* check for 10.6+ only features */)
{
    vc = [NSViewController initWithNibName:@"MyVC-SL" bundle:nil];
}
else
{
    vc = [NSViewController initWithNibName:@"MyVC" bundle:nil];
}
// ...


Not a real answer to your question, apologies, but 2 possible workarounds: isn't it possible to create 2 versions of your xib, and depending on the target, compile on or the other? This would be a bit more work to maintain, but if your UI is pretty stable, this should be the easiest way.

Or you could add your "10.7 specific" UI component(s) programmatically instead of using the IB. If you just have one or a few popovers, it shouldn't be to difficult to do, and the proprocessor guards would work fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜