开发者

Custom UIView subclass as a UIViewController property

I had thought I actually had a pretty good handle on the whole view controller model, but something just doesn't seem to making much sense for me. My main issue is with adding a custom UIView subclass as a property of a UIViewController subclass.

Whenever I assign a valid instance of a UIView subclass to that property, nothing happens or the code crashes.

Here's a quick outline:

  • The main controller inits its own view and that loads fine.
  • I can then add this UIView subclass to the main controller by instantiating it and addSubview:ivar 开发者_Go百科etc. No problems there...

However... if I wanted to have this custom UIView as a property of the ViewController, that does not seem to work. Can anyone shed some light?

Here's a code summary:

@interface CustomUIView : UIView { }

.

@interface MainViewController : UIViewController {
    CustomUIView *someOtherView;
}

@property (nonatomic, copy)  CustomUIView *someOtherView;

...

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];  // the default controller view

    CustomUIView *tmpView = [[CustomUIView alloc] initWithFrame:CGRectMake(0,0,320,480)];

    [self.view addSubview:tmpView];     // this works

    self.someOtherView = tmpView;       // this does NOT work and
    self.view = self.someOtherView;     // ultimately, this is what i'm after

    [tmpView release];

}

Many thanks to this wonderful community!


You can't copy UIViews. Or UIView subclasses. Try retain, instead.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜