用于继承的UIView和UIViewController实践
问题描述:
我只是想知道这种方法是否适用于具有大量自定义视图的应用程序,适用于基于用户交互可能更改的嵌套PNG图形和动画。我创建的延伸的UIView用于继承的UIView和UIViewController实践
@interface BaseView : UIView {
@protected
BaseViewController *controller;
}
@property (retain) BaseViewController *controller;
@end
和相应的控制器类,这是其中我把代码来操纵视图
@interface BaseViewController : UIViewController {
@protected
CGRect drawArea;
}
- (void) drawArea:(CGRect) _drawArea;
- (CGRect) drawArea;
- (void) linkSubviewController: (BaseViewController *) _subviewController;
@end
其中“drawArea”是的CGRect用于将主位置的基本视角类作为一个框架传递给视图。
“linkSubviewController”允许你嵌套的控制器和图如下:
- (void) linkSubviewController: (BaseViewController *) _subviewController {
[self.view addSubview:[_subviewController view]];
}
另外我分层称为“ImageView的”和“ImageViewController”延伸基本视角而且还可以存储一个UIImage和另一个定制对X,Y,W,H
在上欣赏“的drawRect”绘图方法我可以检查以查看是否在self.controller VARS任何瓦尔已被更改,或者分配的图像,例如:
UIImage *image = [(ImageViewController *)self.controller image];
CGContextDrawImage(...) etc
我写大多数的loadView方法是这样的
- (void)loadView {
ImageView *v = [[ImageView new] initWithFrame:drawArea];
v.controller = self;
self.view = v;
}
基部“initWithFrame”例程包含
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
因此,我可以加载各种具有透明背景的图像,而不必分配每个时间。
我已经能够在整个应用程序中使用此代码,并且它似乎可以轻松地编写组装自定义项目布局的*类。对于动画我一直把它们放在控制器中并操作self.view.layer。
基本上我找的反馈,我用新的Objective-C和IPhone SDK
答
这里有几个问题:
- 使用
[[Classname new] init...]
是new
不正确使用。使用new
是[[Classname alloc] init]
的缩写,因此您有效地拨打init
两次。 - 观点应该不需要知道谁在控制他们。
- 您的看法是
retain
控制器,并且由于UIViewController保留其视图,您有一个保留周期,并且都不会完全release
d。
如果你想这种类型的行为(其中一个视图可以委托其父母图纸),尝试创建一个DrawDelegate
协议,有你的控制器执行该协议,并在您的视图子类中有一个非保持drawDelegate
属性:
@protocol DrawDelegate
- (void) drawArea:(CGRect)rect;
@end
@interface BaseView : UIView {
id<DrawDelegate> drawDelegate;
}
@property (assign) id<DrawDelegate> drawDelegate;
@end
是否有任何具体原因使用[Class new] over [[Class alloc] init]?除了明显的扩展initWith和whatnot之外。 – Sneakyness 2009-07-25 15:15:55