iPhone: extender una clase UIView

creato. Quiero mostrar cómo es posible, de una forma sencilla y rápida, ampliar una clase UIView lo que responde a un nuevo método de inicialización que contiene código adicional para personalizar el objeto UIView creada. Como algunos de ustedes probablemente saben, Objective-C no admite un único fabricante como en otros lenguajes orientados a objetos (como Adobe ActionScript o PHP5). E 'puede, de hecho, crear una instancia de una clase manualmente llamando a diversos fabricantes (virtualmente infinita). . Normalmente estamos acostumbrados a crear objetos UIView con el clásico initWithFrame . Supongamos, sin embargo, quieren crear una nueva clase de objetos visuales, partiendo de un UIView , con algunas adiciones, como por ejemplo una etiqueta incorporada. . Básicamente lo que queremos es que nuestro objeto se crea una instancia, así como la UIView , también un objeto de tipo UILabel . Por ejemplo, es posible que desee obtener al final:

1
[ [ ViewLabel alloc ] initWithLabelDefine : CGRectMake ( 0 , 0 , 320 , 80 ) label : @ "Ciao" ] ; ViewLabel * vl = [[ViewLabel alloc] initWithLabelDefine: (0, 0, 320, 80) etiqueta CGRectMake: @ "Hola"];

: Para ello basta con crear una nueva clase de tipo UIView y llamada ViewLabel :

1
2
3
4
5
6
7
8
9
10
11
12
13
/ /
/ / ViewLabel.h
/ /

# Importar <UIKit/UIKit.h>

UIView { @ Interface ViewLabel: UIView {
UILabel * internalLabel;
}

id ) initWithLabelDefine : ( CGRect ) frame label : ( NSString * ) labelDefine; - (Id) initWithLabelDefine: (CGRect) etiqueta de fotograma: ( NSString *) labelDefine;

@ End
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
/ /

# Importar "ViewLabel.h"

@ Implementación ViewLabel

id ) initWithFrame : ( CGRect ) frame { - (Id) initWithFrame: (CGRect) frame {

self = [ super initWithFrame : frame ] ) { if (self = [super initWithFrame: Marco]) {
/ / Código de inicialización
}
volver yo;
}

/ /
/ / Nuestro nuevo inicializador
/ /
id ) initWithLabelDefine : ( CGRect ) frame label : ( NSString * ) labelDefine { - (Id) initWithLabelDefine: (CGRect) etiqueta de fotograma: ( NSString *) {labelDefine

self = [ super initWithFrame : frame ] ) { if (self = [super initWithFrame: Marco]) {
/ / Aquí usted puede decidir cómo comportarse, que para
/ / Seguridad Tengo el mismo período en el pasado
/ / Entrada pero puedo restablecer la x e y
; frame.origin.x = 0;
; frame.origin.y = 0;
UILabel alloc ] initWithFrame : frame ] ; internalLabel = [[UILabel alloc] initWithFrame: Marco];
internalLabel.text = labelDefine;
internalLabel ] ; [Self addSubview: internalLabel];
; [InternalLabel liberación];
}
volver yo;
}

void ) drawRect : ( CGRect ) rect { - (Void) drawRect: (CGRect) rect {
Code / / Dibujo
}

void ) dealloc { - (Void) dealloc {
; [Súper dealloc];
}

@ End

En el momento de la creación de nuestra nueva clase, podríamos utilizar:

1
[ [ ViewLabel alloc ] initWithLabelDefine : CGRectMake ( 0 , 0 , 320 , 80 ) label : @ "Ciao" ] ; ViewLabel * vl = [[ViewLabel alloc] initWithLabelDefine: (0, 0, 320, 80) etiqueta CGRectMake: @ "Hola"];

No hay comentarios para este post

Deja un comentario

TAG XHTML PERMISOS: ENTRADA CÓDIGO:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL