Is there a way to create a NIB file programmatically?
I know it is e开发者_运维问答asy to create a NIB file easily with interface builder but I was wondering if there is a way to archive an existing UIView into a NIB for use later.
It looks like a NIB file is just a special type of bundle where each of the UIView objects are archived using NSCoding but I cannot seem to find any more info beyond that. All the resources I've found talk about using interface builder to create the proper NIB file.
Any help would be appreciated!
I'm afraid you can't. You can archive an UIView
to whatever file, but you can't use it as a nib file.
You can dump the content of a nib file to an Objective-C code using nib2objc, but I guess it's not what you want.
@Yuji.. your answer is INCORRECT. A "nib" and a "view" are like identical twins - that have different "attitudes". They are essentially, exactly, the same.. and could be forced to done to do the same things in the same way.. but they were born in different hospitals, and live with seperate families.
What happens when you "archive".. or "copy" views is fundamentally, and inherently the same as what IB does through the various, and oft-misunderstood ways and means of encode
and decode
, withCoder
, i.e.
- (id)initWithCoder:(NSCoder*)coder {
if ((self = [super initWithCoder:coder])) {
backgroundColor = [coder decodeObjectForKey:@"backgroundColor"];
uniqueID = [coder decodeObjectForKey: @"uniqueID"];
[self updateTrackingAreas];
if (something) [self addSubview:atv];
} return self; }
- (id)copyWithZone:(NSZone *)zone; { return self; }
- (void) encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:backgroundColor forKey:@"backgroundColor"];
[aCoder encodeObject:uniqueID forKey:@"uniqueID"];
[aCoder encodeObject:file forKey:@"file"];
[aCoder encodeObject:atv forKey:@"atv"];
[aCoder encodeBool:hasText forKey:@"hasText"];
}
It's totally a schlemiel schlomozel kind of thing… different strokes for different folks. Xcode happens to be a compiler that is much better reading some crazy XML sort of thing (a NIB) than we are.. while we are used to having our hand with Objective C. Different means to the same end.
Did you think that Xcode was whispering sweet nothings to the assembler that you didn't know about? Don't be jealous!
精彩评论