0

Having just started with Objective-C, this might be a pretty basic question. In objective-c, how can I define constants when extending a default object (say UIViewController). The constant values will then be used by all the objects extending the default object.

One way to do this can be creating categories for the object, something like this...

@implementation UIViewController (Constants)

+ (NSString *)navigationView {
    return @"navigationView";
}
+ (NSString *)detailView {
    return @"detailView";
}

@end

Would using #define values in category implementation also work fine for this??? Or may be some better ways are there to do this?

4

1 回答 1

0

That is fine. That will work as expected, although I would suggest using more appropriate names (the selector navigationView suggests you'll get back an actual view rather than a string with its name in).

You can use #defines if you wish - just define them straight in a header file. Or you can define string constants e.g.

// in the header

extern NSString* const NAVIGATION_VIEW_NAME; // declaration

// in a .m file somewhere

NSString* const NAVIGATION_VIEW_NAME = @"navigationView"; // definition
于 2012-04-26T10:43:05.420 回答