Created
November 18, 2009 11:07
-
-
Save sukima/237747 to your computer and use it in GitHub Desktop.
How to make custom init methods for a UIView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "MyView.h" | |
int main(void) | |
{ | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
Person *p = [[Person alloc] init]; | |
//You could do this: | |
// know that you now much seperate initialization code into viewWillApear | |
// and viewDidLoad so that things don't happen before you assign the person | |
// to it. (easy to create errors. | |
MyView *view1 = [[MyView alloc] initWithNibName:@"MyViewNibFile" bundle:nil]; | |
view1.myPerson = p; | |
// Display view1. | |
// A better way with hard coded name: | |
MyView *view2 = [[MyView alloc] initWithPerson:p]; | |
// Display view2. | |
// Probubly the best way: | |
MyView *view3 = [[MyView alloc] initWithPerson:p andNibName:@"MyViewNibFile" | |
bundle:nil]; | |
// Display view3. | |
[view3 release]; | |
[view2 release]; | |
[view1 release]; | |
[p release]; | |
[pool release]; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MyView.h | |
#import <UIKit/UIKit.h> | |
#import "Person.h" | |
@interface MyView : UIView { | |
Person *myPerson; | |
} | |
// Setup a property so we can programatically set the data for this view | |
@property (nonatomic, retain) Person *myPerson; | |
// Custom init method | |
- (id)initWithPerson:(Person *)person; | |
// A different way to do a custom init | |
- (id)initWithPerson:(Person *)person andNibName:(NSString *)nib | |
bundle:(NSBundle *)bundle; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MyView.m | |
#import "MyView.h" | |
@implementation MyView | |
@synthesize myPerson; | |
// This way hard codes the nib file name in the code. Probubly bad but in some | |
// cases could be good. | |
- (id)initWithPerson:(Person *)person; | |
{ | |
if ((self = [super initWithNibName:@"MyViewNibFile" bundle:nil]) == nil) | |
return nil; | |
self.myPerson = person; | |
return self; | |
} | |
// This is more what might work for you. | |
- (id)initWithPerson:(Person *)person andNibName:(NSString *)nib | |
bundle:(NSBundle *)bundle; | |
{ | |
if ((self = [super initWithNibName:nib bundle:bundle]) == nil) | |
return nil; | |
self.myPerson = person; | |
return self; | |
} | |
- (void)dealloc; | |
{ | |
[myPerson release]; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment