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?
That is fine. That will work as expected, although I would suggest using more appropriate names (the selector
navigationViewsuggests you’ll get back an actual view rather than a string with its name in).You can use
#definesif you wish – just define them straight in a header file. Or you can define string constants e.g.