I have a custom view that I want to be able to initialize both in-code and in nib.
2014-12-04 11:16
561 查看
Objective
C - Custom view and implementing init method?
http://stackoverflow.com/questions/7226239/objective-c-custom-view-and-implementing-init-method![](http://static.adzerk.net/Advertisers/092148373a314bd88a621559b7cc0b46.png)
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiYnQiOjAsImNtIjoxODY2MjQsImNoIjoxMTc4LCJjciI6NjYyODEyLCJkaSI6ImUwZDc0OWNiOWM1MDQzM2FhNDZjYzdhODNlMjI3NTQ1IiwiZG0iOjEsImZjIjo2ODQ1MzQsImZsIjo0MTAxODMsImlwIjoiMTI0LjI0OC4yMjEuMTU1Iiwia3ciOiJvYmplY3RpdmUtYyxpbml0d2l0aGZyYW1lLGluaXR3aXRoY29kZXIiLCJudyI6MjIsInBjIjowLCJwciI6MTU2OCwicnQiOjEsInJmIjoiaHR0cDovL3d3dy5nb29nbGUuY29tLmhrL3VybD9zYT10JnJjdD1qJnE9JmVzcmM9cyZzb3VyY2U9d2ViJmNkPTMmdmVkPTBDQ2tRRmpBQyZ1cmw9aHR0cDovL3N0YWNrb3ZlcmZsb3cuY29tL3F1ZXN0aW9ucy83MjI2MjM5L29iamVjdGl2ZS1jLWN1c3RvbS12aWV3LWFuZC1pbXBsZW1lbnRpbmctaW5pdC1tZXRob2QmZWk9QTg5X1ZPYVJMS1hhbWdXZHRZR1lCUSZ1c2c9QUZRakNOSE9NbXd2ZHRKbWRlNnlxTzRfaDVYZk51cUhiUSZidm09YnYuODA2NDIwNjMsZC5kR1kmY2FkPXJqdCIsInN0Ijo4Mjc3LCJ1ayI6InVlMS0yMjRiMjBjZmE2MGU0MDE1YjhjZjY5YmFjODg4MzFmNyIsInpuIjo0MywidHMiOjE0MTc2NjI4NTIyNjYsImJmIjp0cnVlfQ&s=I4rjZ1RZMdDJMqMhslxXpy52RdY)
up vote16down votefavorite 15 | I have a custom view that I want to be able to initialize both in-codeand in nib. What's the correct way to write both initWithFrameand initWithCodermethods? They both share a block of code that is used for some initialization. objective-c initwithframe initwithcoder
| ||
add a comment |
3 Answers
activeoldestvotesup vote30down voteaccepted | The right thing to do in that case is to create another method containing the code that's common to both -initWithFrame:and -initWithCoder:, and then call that method from both -initWithFrame:and -initWithCoder:: - (void)commonInit { // do any initialization that's common to both -initWithFrame: // and -initWithCoder: in this method } - (id)initWithFrame:(CGRect)aRect { if ((self = [super initWithFrame:aRect])) { [self commonInit]; } return self; } - (id)initWithCoder:(NSCoder*)coder { if ((self = [super initWithCoder:coder])) { [self commonInit]; } return self; } Do heed the concerns outlined in Justin's answer, particularly that any subclasses must not override -commonInit. I used that name here for its illustrative value, but you'll probably want one that's more closely tied to your class and less likely to be accidentally overridden. If you're creating a purpose-built UIView subclass that's unlikely to be subclassed itself, using a common initialization method as above is perfectly fine. If you're writing a framework for others to use, or if you don't understand the issue but want to do the safest possible thing, use a static function instead.
|
相关文章推荐
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- 【Android笔记】引用第三方自定义控件出错:Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
- Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
- Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE
- Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
- PH FIN want to have PR/PO in TWD and SGD but do invoice matching and payment in USD
- How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?
- Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE
- rror Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnec
- Statistics doesn’t have to be so hard! Resampling in R and SAS
- Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
- 使用eclipse编程出现Use View.isInEditMode() in your custom views to skip code when shown in Eclipse解决方法
- 【转】WARNING! File system needs to be upgraded. You have version null and I want version 7. Run the '${HBASE_HOME}/bin/hbase migrate' script. 的解决办法
- File system needs to be upgraded. You have version null and I want version 7
- Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE
- Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
- Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
- Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse