在 UIViewController 中设置子视图
编辑:这个问题是由于严重缺乏对 Interface Builder 和类中的属性如何工作的理解。
为什么我不能像设置 self.view = anotherView;
那样设置 self.mySubView = anotherhView;
?
## .h
@interface TestController : UIViewController {
IBOutlet UIView *mySubView;
}
@property (nonatomic, retain) IBOutlet UIView *mySubView;
##.m
@implements TestController
@synthesize mySubView;
- (void)viewDidLoad {
AnotherController *anotherController = [[AnotherController alloc] initWithNibName:nil bundle:nil];
anotherView = anotherController.view;
// if i do
self.view = anotherView;
// result: replaces whole view with anotherView
// if i instead do
self.mySubView = anotherView;
// result: no change at all
// or if i instead do:
[self.mySubView addSubview:anotherView];
// result: mySubView is now displaying anotherView
}
注意:我正在使用界面构建器。我确信一切都连接正常,因为 self.view 和 self.mySubView addSubview: 工作正常..
EDIT: This question is due to a big lack of understanding how Interface Builder and properties in classes works.
Why can't i just set self.mySubView = anoterhView;
like one can set self.view = anotherView;
?
## .h
@interface TestController : UIViewController {
IBOutlet UIView *mySubView;
}
@property (nonatomic, retain) IBOutlet UIView *mySubView;
##.m
@implements TestController
@synthesize mySubView;
- (void)viewDidLoad {
AnotherController *anotherController = [[AnotherController alloc] initWithNibName:nil bundle:nil];
anotherView = anotherController.view;
// if i do
self.view = anotherView;
// result: replaces whole view with anotherView
// if i instead do
self.mySubView = anotherView;
// result: no change at all
// or if i instead do:
[self.mySubView addSubview:anotherView];
// result: mySubView is now displaying anotherView
}
NOTE: I'm using interfacebuilder. I'm sure everything is hooked up allright because self.view, and self.mySubView addSubview: is working allright..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要使其自动出现在您的 self.view 上,您需要覆盖您的 setter 方法,例如:
To make it automatically appear on your
self.view
you need to overwrite your setter method, e.g.:mySubview 是一个对 UIView 对象的引用的属性。因此,当您为其分配 UIView 对象时,您只是更改了 mySubview 所指的内容,而不再像本例中那样,
mySubview 的原始 UIView 对象 所引用的内容仍然在 view 的 subviews 属性中引用。没有任何改变。
但是,当您添加 anotherView 作为 mySubview 的子视图时,anotherView 属于视图层次结构并显示在屏幕上。所以这有效。
但是,当您将 anotherView 直接分配给 view 时,您不仅会更改 view 所引用的 UIView 对象,还会将其自身添加到 ParentView 中。这是由 UIViewController 处理的。
您的 setCurrentView 应该或多或少像这样,
mySubview is a property which is a reference to an UIView object. So when you assign an UIView object to it, you are merely changing what mySubview is referring to and no more as in this case,
The original UIView object that mySubview was referring to is still referred to within view's subviews property. Nothing changes.
But when you add anotherView as a subview of mySubview, anotherView belongs to the view hierarchy and is displayed on screen. So this works.
However when you assign anotherView directly to the view, You not only change the UIView object view was referring to but it also adds itself to the parentView. This is handled by UIViewController.
Your setCurrentView should be more or so like this,
作为对@beefon所说的回应。这有点像预期的那样,但背景颜色是透明的。它也没有响应...按钮没有被按下等等...
As a response to what @beefon said. This works kind of as expected, but background-color is transparent. It doesen't respond either... buttons do not get pressed etc..
您的实例变量需要是一个属性才能使用点。语法,
在标头中使用:,
在主文件中使用:。
为了使用点设置 UIView。语法你需要使它成为一个属性。这还允许您在类之外设置子视图的属性。
Your instance variable needs to be a property in order to use the dot. syntax, use:
in the header, and use:
in the main file.
In order to set a UIView using the dot. syntax you need to make it a property. This also allows you to set the
subview
's properties outside the class.