iPhone: estender uma classe UIView

creato. Eu quero mostrar como é possível, de forma simples e rápida, estender uma classe UIView de modo que responde a um novo método de inicialização que contém o código adicional para personalizar o objeto UIView criado. Como alguns de vocês provavelmente sabem, Objective-C não oferece suporte a um único fabricante como em outras linguagens orientadas a objeto (como o Adobe ActionScript ou PHP5). E 'pode, de fato, criar uma instância de uma classe manualmente chamando vários fabricantes (virtualmente infinita). . Normalmente são usados ​​para criar objetos UIView com o clássico initWithFrame . Suponha-se, no entanto, querem criar uma nova classe de objetos visuais, a partir de um UIView , com algumas adições, por exemplo, um rótulo incorporado. . Basicamente, o que nós queremos é que o nosso objeto instanciar isso cria, bem como o UIView , também um objeto do tipo UILabel . Por exemplo, pode querer começar no final:

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

: Para conseguir isso basta criar uma nova classe de tipo UIView e chamada 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) rótulo de quadro: ( 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
/ /

# Import "ViewLabel.h"

@ Implementação ViewLabel

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

self = [ super initWithFrame : frame ] ) { if (self = [super initWithFrame: quadro]) {
/ / Inicialização de código
}
retornar auto;
}

/ /
/ / Nosso novo inicializador
/ /
id ) initWithLabelDefine : ( CGRect ) frame label : ( NSString * ) labelDefine { - (Id) initWithLabelDefine: (CGRect) rótulo de quadro: ( NSString *) {labelDefine

self = [ super initWithFrame : frame ] ) { if (self = [super initWithFrame: quadro]) {
/ / Aqui você pode decidir como se comportar, eu para
/ / Segurança eu tenho o mesmo quadro no passado
/ / Entrada mas eu redefinir a x e y
; frame.origin.x = 0;
; frame.origin.y = 0;
UILabel alloc ] initWithFrame : frame ] ; internalLabel = [[UILabel alloc] initWithFrame: quadro];
internalLabel.text = labelDefine;
internalLabel ] ; [Auto addSubview: internalLabel];
; [InternalLabel release];
}
retornar auto;
}

void ) drawRect : ( CGRect ) rect { - (Void) drawRect: (CGRect) {rect
Código / / Desenho
}

void ) dealloc { - (Void) {dealloc
; [Super dealloc];
}

@ End

Na altura da criação da nova classe, poderíamos utilizar:

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

Não há comentários para este post

Deixe um comentário

TAG XHTML permita: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> código de entrada:
 <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