ios - 从 UIWindow 中移除 UIViewController 的 View

标签 ios objective-c uiwindow

当用户单击添加按钮时,我将在 UIWindow 上添加 View 。现在,如果用户再次点击添加按钮,我想先删除该 View 并再次添加。

我已使用此代码在 UIWindow 上添加 View :

ProgressVC *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"IDProgressVC"];
[[[[UIApplication sharedApplication] delegate]window] addSubview:vc.view];

对于删除,我已经尝试过此代码:

[vc.view removeFromSuperview];
[[[[UIApplication sharedApplication] delegate]window] setNeedsLayout];
vc = nil;

我们将不胜感激。

最佳答案

试试这个

// Make this global property

@property(nonatomic, strong) UIView * currentView;

//store the view in gloabal property
ProgressVC *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"IDProgressVC"];
currentView = vc.view;
[[[[UIApplication sharedApplication] delegate]window] addSubview:currentView];

//remove it 
[currentView removeFromSuperview];
[[[[UIApplication sharedApplication] delegate]window] setNeedsLayout];
currentView = nil;

关于ios - 从 UIWindow 中移除 UIViewController 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38100956/

相关文章:

ios - 调整 UIView 的大小以便为广告单元留出空间

iphone - iOS 中的内存崩溃,实际内存使用量仅为 5megs

ios - 如何在 Swift 中查找文件类型

ios - RLMObjects 在 Realm Cocoa 中未被识别为相同的对象

objective-c - 为什么断言不是NSFetchRequest的属性?

ios - 为什么 UIWebViewDelegate 分配而不是弱?

ios - 什么时候是将 CALayers/CAGradientLayers 添加到 iOS 8.3 的 UITableViewCells 或 UICollectionViewCells 的最佳方式?

ios - NSInteger 前面的那个符号是什么?

ios - UIWindow endDisablingInterfaceAutorotationAnimated : error

ios - 为什么double类型的主窗口是可选的?