creato. I want to show how it is possible, in a simple and rapid, extend a class UIView so that responds to a new initialization method containing additional code to customize the object UIView created. As some of you probably know, Objective-C does not support a single manufacturer as in other object oriented languages (such as Adobe ActionScript or PHP5). E 'can, in fact, create an instance of a class manually by calling various manufacturers (virtually infinite). . Normally we are used to create objects UIView with the classic initWithFrame . Suppose, however, want to create a new class of visual objects, starting from a UIView , with some additions, for example a label incorporated. . Basically what we want is that our object instantiating this creates, as well as the UIView , also an object of type UILabel . For example, we may want to get at the end:
1 | [ [ ViewLabel alloc ] initWithLabelDefine : CGRectMake ( 0 , 0 , 320 , 80 ) label : @ "Ciao" ] ; ViewLabel * vl = [[ViewLabel alloc] initWithLabelDefine: CGRectMake (0, 0, 320, 80) label: @ "Hello"]; |
: To achieve this simply create a new class of type UIView and call ViewLabel :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | / / / / ViewLabel.m / / # Import "ViewLabel.h" @ Implementation ViewLabel id ) initWithFrame : ( CGRect ) frame { - (Id) initWithFrame: (CGRect) frame { self = [ super initWithFrame : frame ] ) { if (self = [super initWithFrame: frame]) { / / Initialization code } return self; } / / / / Our new initializer / / id ) initWithLabelDefine : ( CGRect ) frame label : ( NSString * ) labelDefine { - (Id) initWithLabelDefine: (CGRect) frame label: ( NSString *) {labelDefine self = [ super initWithFrame : frame ] ) { if (self = [super initWithFrame: frame]) { / / Here you can decide how to behave, I for / / Security I got the same frame in the past / / Input but I reset the x and y ; frame.origin.x = 0; ; frame.origin.y = 0; UILabel alloc ] initWithFrame : frame ] ; internalLabel = [[UILabel alloc] initWithFrame: frame]; internalLabel.text = labelDefine; internalLabel ] ; [Self addSubview: internalLabel]; ; [InternalLabel release]; } return self; } void ) drawRect : ( CGRect ) rect { - (Void) drawRect: (CGRect) rect { / / Drawing code } void ) dealloc { - (Void) dealloc { ; [Super dealloc]; } @ End |
At the time of the creation of our new class, we could use:
1 | [ [ ViewLabel alloc ] initWithLabelDefine : CGRectMake ( 0 , 0 , 320 , 80 ) label : @ "Ciao" ] ; ViewLabel * vl = [[ViewLabel alloc] initWithLabelDefine: CGRectMake (0, 0, 320, 80) label: @ "Hello"]; |










There are no comments for this post
Leave a comment