views:

495

answers:

1

Hello. 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 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!

+3  A: 

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

chpwn
son of a... yep that solved it. many, many thanks! in selecting the property attributes, i did consider that, but chose copy instead. why is copy not allowed on UIView? this is especially confusing because UILabel, UIButton, etc inherit from UIView as well and those can be "copied".
bitcruncher
If it solved please mark this answer as the correct one and give him an upvote
willcodejavaforfood