为什么在重新分配后需要 remove/add NSView 才能显示
why need remove/add a NSView after it's reassigned to make it displayed
我发现,当我将一个 NSView(viewA aleardy added into superView) 重新分配给另一个视图(new viewB) 时,如下所示:
viewA = [[NSView alloc] init];
viewB = [[NSView alloc] init];
[self.view addSubview viewA];
viewA = viewB;
viewB不会更新成superView,事件我尝试了以下方法:
1)
[viewA setHidden: YES];
[viewA setHidded: NO];
2)
[viewA setNeedLayout: YES];
只有删除 viewA 并将其重新添加回它的超级视图才有效:
[viewA removeFromSuperView];
[self.view addSubview:viewA];
谁能帮忙解释一下为什么方法 1) 和 2) 无法更新 viewA 的矩形?
如果要查看视图,您必须将视图添加到视图层次结构中。 viewA = viewB;
使变量 viewA
指向与 viewB
相同的视图,它不会将 viewB
添加到视图层次结构。
这是您的代码的作用:
viewA = [[NSView alloc] init]; // viewA points to a new view(A)
viewB = [[NSView alloc] init]; // viewB points to a new view(B)
[self.view addSubview viewA]; // view(A) is added to the view hierarchy and will be displayed
viewA = viewB; // variable viewA points to view(B)
[viewA setHidden: YES]; // hides view(B)
[viewA setHidded: NO]; // unhides view(B), but view(B) isn't visible because it isn't part of the view hierarchy
[viewA setNeedLayout: YES]; // doesn't do anything, view(B) isn't visible because it isn't part of the view hierarchy
[viewA removeFromSuperView]; // doesn't do anything, view(B) isn't in a superview
[self.view addSubview:viewA]; // view(B) is added to the view hierarchy and will be displayed
解决方法:
将视图 (B) 添加到视图层次结构
viewA = [[NSView alloc] init]; // viewA points to a new view(A)
viewB = [[NSView alloc] init]; // viewB points to a new view(B)
[self.view addSubview viewA]; // view(A) is added to the view hierarchy and will be displayed
[self.view addSubview viewB]; // view(B) is added to the view hierarchy and will be displayed
我发现,当我将一个 NSView(viewA aleardy added into superView) 重新分配给另一个视图(new viewB) 时,如下所示:
viewA = [[NSView alloc] init];
viewB = [[NSView alloc] init];
[self.view addSubview viewA];
viewA = viewB;
viewB不会更新成superView,事件我尝试了以下方法:
1)
[viewA setHidden: YES];
[viewA setHidded: NO];
2)
[viewA setNeedLayout: YES];
只有删除 viewA 并将其重新添加回它的超级视图才有效:
[viewA removeFromSuperView];
[self.view addSubview:viewA];
谁能帮忙解释一下为什么方法 1) 和 2) 无法更新 viewA 的矩形?
如果要查看视图,您必须将视图添加到视图层次结构中。 viewA = viewB;
使变量 viewA
指向与 viewB
相同的视图,它不会将 viewB
添加到视图层次结构。
这是您的代码的作用:
viewA = [[NSView alloc] init]; // viewA points to a new view(A)
viewB = [[NSView alloc] init]; // viewB points to a new view(B)
[self.view addSubview viewA]; // view(A) is added to the view hierarchy and will be displayed
viewA = viewB; // variable viewA points to view(B)
[viewA setHidden: YES]; // hides view(B)
[viewA setHidded: NO]; // unhides view(B), but view(B) isn't visible because it isn't part of the view hierarchy
[viewA setNeedLayout: YES]; // doesn't do anything, view(B) isn't visible because it isn't part of the view hierarchy
[viewA removeFromSuperView]; // doesn't do anything, view(B) isn't in a superview
[self.view addSubview:viewA]; // view(B) is added to the view hierarchy and will be displayed
解决方法: 将视图 (B) 添加到视图层次结构
viewA = [[NSView alloc] init]; // viewA points to a new view(A)
viewB = [[NSView alloc] init]; // viewB points to a new view(B)
[self.view addSubview viewA]; // view(A) is added to the view hierarchy and will be displayed
[self.view addSubview viewB]; // view(B) is added to the view hierarchy and will be displayed