Re: initWithCoder but I do not have nib file
Re: initWithCoder but I do not have nib file
- Subject: Re: initWithCoder but I do not have nib file
- From: Chase Meadors <email@hidden>
- Date: Sat, 15 Aug 2009 09:42:51 -0500
Looks like you don't quite understand -init/initWithCoder methods. Why
are you passing UIButtonTypeRoundedRect as the (NSCoder *)coder
argument?
-initWithCoder only exists to be called when an object is created in a
nib.
From the documentation on the NSCoding protocol (which includes
initWithCoder),
"The NSCoding protocol declares the two methods that a class must
implement so that instances of that class can be encoded and decoded.
This capability provides the basis for archiving (where objects and
other structures are stored on disk) and distribution (where objects
are copied to different address spaces)."
encoding and decoding are what happens when objects are created in a
nib. So basically, the argument to initWithCoder is something that
interface builder uses to unarchive an object in a nib. You simply
have to implement it and call whatever regular init method you want.
The idea is to do something like this in a custom class:
- (id)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
return [NSButton buttonWithType:UIButtonTypeRoundedRect];
}
}
From the documentation on the NSCoding protocol (which includes
initWithCoder),
"The NSCoding protocol declares the two methods that a class must
implement so that instances of that class can be encoded and decoded.
This capability provides the basis for archiving (where objects and
other structures are stored on disk) and distribution (where objects
are copied to different address spaces)."
encoding and decoding are what happens when objects are created in a
nib. So basically, the argument to initWithCoder is something that
interface builder uses to unarchive an object in a nib. You simply
have to implement it and call your regular init method.
If you're not using a nib, simply create a new button with
[CustomButton buttonWithType:UIButtonTypeRoundedRect]; You probably
need to override -buttonWithType to add custom behavior to your class.
Did that make sense?
On Aug 15, 2009, at 5:07 AM, Agha Khan wrote:
Hi:
I was looking a blog about customButton
http://supergravelynbros.com/?p=871
The author explained that initializing with
- (id)initWithCoder:(NSCoder *)coder
I implemented something like this
self.helpButton = [CustomButton
initWithCoder:UIButtonTypeRoundedRect];
which complies OK, but get exception at this point.
I know the reason I do not have a nib file, but I believe I don't
need it.
According to author
The initWithCoder: method is what will be called when the game reads
the button in from the nib files.
But I do not have any nib file in my project. What can we do in such
situation?
Best regards
-Agha
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden