假设我有AViewcontrollers
和BviewControllers
。我将BViewcontrollers视图添加为Aviewcontrollers
视图的子视图。我想从BviewController
视图中删除AViewController
视图。
我正在尝试以这种方式删除' b视图'但它没有成功。
AViewcontrollers *aVC = [[AviewControllers alloc] initWithNibName:@"AViewcontrollers" bundle:nil];
BViewcontrollers *bVC = [[BviewControllers alloc] initWithNibName:@"BViewcontrollers" bundle:nil];;
[aVC addsubView:bVc.view];
//Trying to remove the 'b view' from the 'a view'
for (UIView *subview in [[aVC view] subviews] ) {
NSLog(@"Subview::%@",[subview class]);
if ([subview isMemberOfClass:[BViewControllers class]]) {
[subview removeFromSuperview];
NSLog(@"This subview Belongs to tools view controller");
} }
答案 0 :(得分:4)
显而易见的答案是:
[bVC.view removeFromSuperview];
上面代码的问题在于您正在检查视图是BviewControllers
类,而不是BviewControllers
的视图。如果视图有一个特定的类(比如说Bview
),那么你可以进行检查(if ([subview isMemberOfClass:[BView class]])
)。
使用isMemberOfClass:
也非常具体,因此您最好使用isKindOfClass:
答案 1 :(得分:0)
希望你的bViewController有一个对视图的引用?如果是这样,你可以打电话:
[bVC.view removeFromSuperview]
答案 2 :(得分:0)
考虑将BViewController添加为AViewController的子节点,此外还将B的视图添加为A的子视图。这样做可以转发自转事件等等;类似的东西:
AViewController *aVC = [[UIViewController alloc] init];
BViewController *bVC = [[UIViewController alloc] init];
[aVC addChildViewController:bVC];
[bVC didMoveToParentViewController:aVC];
[aVC.view addSubview:bVC.view];
然后,当您想要删除视图(和控制器)时,您可以遍历子视图控制器。 (或以其他方式存储指针)
for (UIViewController *vc in aVC.childViewControllers){
if ([vc isKindOfClass:[BViewController class]]){
[vc.view removeFromSuperview];
[vc removeFromParentViewController];
[vc didMoveToParentViewController:nil];
}
}