在我的UIViewController子类中存储MyView
的转换引用是否有任何危险?我每次想要使用实例变量/方法时都试图避免转换self.view
,因为它看起来很难看。例如,让我们避免:
[((MyView *)self.view) instancedView]
castedView
与self.view
castedView.instancedView
是castedView
的子视图,存储为实例变量(例如,我想附加点击事件监听器的UIButton)
@interface MyViewController : UIViewController
@property MyView *castedView;
@end
@implementation MyViewController
@synthesize castedView;
- (void)viewDidLoad {
[super viewDidLoad];
castedView = [[MyView alloc] init];
self.view = castedView;
// ... create gestureRecognizer named "onTap" that calls "whenTapped" ...
[castedView.instancedView addGestureRecognizer:onTap];
}
- (IBAction)whenTapped {
castedView.instancedView.backgroundColor = [UIColor whiteColor];
}
@end
感谢任何帮助。