简体   繁体   English

自定义UIView子类作为UIViewController属性

[英]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. 我的主要问题是添加自定义UIView子类作为UIViewController子类的属性

Whenever I assign a valid instance of a UIView subclass to that property, nothing happens or the code crashes. 每当我将UIView子类的有效实例分配给该属性时,没有任何反应或代码崩溃。

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... 然后我可以通过实例化它和addSubview:ivar等将这个UIView子类添加到主控制器......那里没有问题...

However... if I wanted to have this custom UIView as a property of the ViewController, that does not seem to work. 但是...如果我想将这个自定义UIView作为ViewController的属性 ,那似乎不起作用。 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 UIView s. 你无法复制UIView Or UIView subclasses. UIView子类。 Try retain, instead. 请尝试保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM