从任何其他类的控制器中删除视图?
Posted
技术标签:
【中文标题】从任何其他类的控制器中删除视图?【英文标题】:Remove a view from controller from any other class? 【发布时间】:2016-08-11 09:36:44 【问题描述】:我有三个屏幕谷歌地图,设置,登录。默认情况下谷歌地图是可见的。当我想显示设置屏幕时,我不会移动到设置视图控制器,我只是将“设置”屏幕添加为“谷歌”的子视图地图"屏幕。
menusettings=[self.storyboard instantiateViewControllerWithIdentifier:@"SET"];
[menusettings.view setFrame:CGRectMake(0, 0, 700,600)];
[self.view addSubview:menusettings.view];
更新设置后,我移动到登录屏幕
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
UINavigationController *ppc = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:@"LOGIN"];
[self presentViewController:ppc animated:true completion:nil];
但是设置屏幕仍然在登录屏幕之后,但它没有被删除。我尝试了两个场景
1.我尝试在进入登录屏幕之前删除设置视图
[self.view removeFromSuperview];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"PRG_iPhone" bundle:nil];
UINavigationController *ppc = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:@"LOGIN"];
[self presentViewController:ppc animated:true completion:nil];
但它不会移动到登录屏幕。
2.当我移动到登录屏幕后尝试删除设置视图时
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"PRG_iPhone" bundle:nil];
UINavigationController *ppc = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:@"LOGIN"];
[self presentViewController:ppc animated:true completion:nil];
[self.view removefromsuperview];
然后它移动到登录屏幕但不删除设置视图
所以我想为什么不从 登录屏幕 中删除谷歌地图屏幕的子视图,它是 设置屏幕,但我不知道如何删除 谷歌地图屏幕子视图是设置屏幕。请指导如何做到这一点?
【问题讨论】:
你想隐藏菜单设置视图吗? 我想从superview中删除菜单设置。当登录屏幕出现时它已经隐藏了 【参考方案1】:改变这个
[self.view removeFromSuperview];
去试试
[menusettings.view removeFromSuperview];
更新
[menusettings.view removeFromSuperview];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"PRG_iPhone" bundle:nil];
UINavigationController *ppc = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:@"LOGIN"];
[self presentViewController:ppc animated:true completion:nil];
选择 1
[self.view.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
选择-2
最初为您的视图分配标签,例如
menusettings=[self.storyboard instantiateViewControllerWithIdentifier:@"SET"];
[menusettings.view setFrame:CGRectMake(0, 0, 700,600)];
menusettings.tag = 5; //you can use any number you like
[self.view addSubview:menusettings.view];
移除
UIView *Remove = [self.view viewWithTag:5];
[Remove removeFromSuperview];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"PRG_iPhone" bundle:nil];
UINavigationController *ppc = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:@"LOGIN"];
[self presentViewController:ppc animated:true completion:nil];
选择 3
for (UIView *subVie in self.view.subviews)
if (subVie.tag == 5)
[subVie removeFromSuperview];
【讨论】:
但是我在设置类中写 [self.view removefromsuperview] 那么我该如何写 [menusettings.view removefromsuperview]; @deepakkumar - 检查 Choice-1 答案 它的作用是从谷歌地图屏幕中删除所有视图。您会看到谷歌地图屏幕已经有视图。应该只删除设置屏幕而不是所有视图。因此使用您的代码它会使谷歌地图屏幕变白 @deepakkumar - 它删除所有子视图,您只需要删除正确的菜单视图 @deepakkumar - 尝试选择 -2以上是关于从任何其他类的控制器中删除视图?的主要内容,如果未能解决你的问题,请参考以下文章