【发布时间】:2011-01-14 15:17:58
【问题描述】:
我以为我实际上对整个视图控制器模型有一个很好的处理,但有些东西对我来说似乎没有多大意义。我的主要问题是将自定义 UIView 子类添加为 UIViewController 子类的 property。
每当我将 UIView 子类的有效实例分配给该属性时,什么都不会发生或代码崩溃。
这里有一个简短的大纲:
- 主控制器会启动自己的视图并且加载正常。
- 然后我可以通过实例化它和
addSubview:ivar等将这个 UIView 子类添加到主控制器。没有问题...
但是...如果我想将此自定义 UIView 作为 ViewController 的 property,那似乎不起作用。有人能解释一下吗?
这里是代码摘要:
@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];
}
非常感谢这个精彩的社区!
【问题讨论】:
标签: iphone objective-c properties uiview uiviewcontroller