在 UIViewController 中设置子视图

发布于 2024-08-20 02:20:17 字数 962 浏览 7 评论 0原文

编辑:这个问题是由于严重缺乏对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还在原地等你 2024-08-27 02:20:17

要使其自动出现在您的 self.view 上,您需要覆盖您的 setter 方法,例如:

- (void)setMySubView:(UIView *)view {
    [mySubView removeFromSuperview];  // removing previous view from self.view
    [mySubView autorelease];
    mySubView = [view retain];
    [self.view addSubview: mySubView]; // adding new view to self.view
}

To make it automatically appear on your self.view you need to overwrite your setter method, e.g.:

- (void)setMySubView:(UIView *)view {
    [mySubView removeFromSuperview];  // removing previous view from self.view
    [mySubView autorelease];
    mySubView = [view retain];
    [self.view addSubview: mySubView]; // adding new view to self.view
}
苍白女子 2024-08-27 02:20:17

mySubview 是一个对 UIView 对象的引用的属性。因此,当您为其分配 UIView 对象时,您只是更改了 mySubview 所指的内容,而不再像本例中那样,

self.mySubview = anotherView;

mySubview 的原始 UIView 对象 所引用的内容仍然在 viewsubviews 属性中引用。没有任何改变。

但是,当您添加 anotherView 作为 mySubview 的子视图时,anotherView 属于视图层次结构并显示在屏幕上。所以这有效。

view (parent of) mySubview (parent of) anotherView

但是,当您将 anotherView 直接分配给 view 时,您不仅会更改 view 所引用的 UIView 对象,还会将其自身添加到 ParentView 中。这是由 UIViewController 处理的。

self.view = anotherView;

您的 setCurrentView 应该或多或少像这样,

- (void) replaceSubview:(UIView *)newView {
  CGRect frame = mySubview.frame;

  [mySubview removeFromSuperview];
  self.mySubview = newView;

  [self.view addSubview:newView];
  newView.frame = frame;
}

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,

self.mySubview = anotherView;

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.

view (parent of) mySubview (parent of) anotherView

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.

self.view = anotherView;

Your setCurrentView should be more or so like this,

- (void) replaceSubview:(UIView *)newView {
  CGRect frame = mySubview.frame;

  [mySubview removeFromSuperview];
  self.mySubview = newView;

  [self.view addSubview:newView];
  newView.frame = frame;
}

仅冇旳回忆 2024-08-27 02:20:17

作为对@beefon所说的回应。这有点像预期的那样,但背景颜色是透明的。它也没有响应...按钮没有被按下等等...

- (void)setCurrentView:(UIView *)newView {
    /*      1. save current view.frame: CGRect mySubViewFrame = [mySubView frame]; 
            2. remove and put new subview - I have wrote how to do it 
            3. set new frame for new view: [mySubView setFrame:mySubViewFrame];      */ 
    CGRect currentViewFrame = [currentView frame];
    [currentView removeFromSuperview];
    [currentView autorelease];
    currentView = [newView retain];
    [self.view addSubview:currentView];
    [currentView setFrame:currentViewFrame]; 
}

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

- (void)setCurrentView:(UIView *)newView {
    /*      1. save current view.frame: CGRect mySubViewFrame = [mySubView frame]; 
            2. remove and put new subview - I have wrote how to do it 
            3. set new frame for new view: [mySubView setFrame:mySubViewFrame];      */ 
    CGRect currentViewFrame = [currentView frame];
    [currentView removeFromSuperview];
    [currentView autorelease];
    currentView = [newView retain];
    [self.view addSubview:currentView];
    [currentView setFrame:currentViewFrame]; 
}
月棠 2024-08-27 02:20:17

您的实例变量需要是一个属性才能使用点。语法,

@Property (nonatomic, retain) IBOutlet UIView* subview;

在标头中使用:,

@synthesize subview;

在主文件中使用:。

为了使用点设置 UIView。语法你需要使它成为一个属性。这还允许您在类之外设置子视图的属性。

Your instance variable needs to be a property in order to use the dot. syntax, use:

@Property (nonatomic, retain) IBOutlet UIView* subview;

in the header, and use:

@synthesize subview;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文