开发者

Why isn’t UIViewController’s view typed as id?

I have always thought it a pity to have the UIViewController’s view explicitly typed as UIView, because the controller subclasses have their own view types and have to recast the UIView to the correct type:

@interface MyController : UIViewController {}
@implementation MyController

- (void) viewDidLoad {
    [(MyView*)self.view doThisO开发者_如何学CrThat];
}

// Or define a custom accessor:
- (MyView*) myView {
    return (MyView*) [self view];
}

This feels dumb. If the UIViewController’s view was typed as id, we could get away without the casting. As the view controllers are made for subclassing, this would seem like a natural design choise. What would be the drawbacks? (Or is there an easier way to overcome the explicit view type problems?)


Using id just because it allows you to call any method without compiler warning is a bad idea, to my taste. You can easily call a method which is not defined even in your view subclass without any compile time warning, later leading to a crash.

I would make another IBOutlet MyView* myView and connect my view not only to view but also to myView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜