从 UIWindow 中移除 UIViewController 的视图
Remove view of UIViewController from UIWindow
当用户单击添加按钮时,我正在 UIWindow 上添加视图。现在,如果用户再次单击添加按钮,我想先删除该视图并再次添加它。
我已经使用此代码在 UIWindow 上添加视图:
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;
任何帮助将不胜感激。
确保 vc
是同一个对象,没有区别。
我的意思是,当您添加 vc.view 时,您会分配和初始化对象,当您删除时,您会再次分配和初始化,然后两者都是相同 class.[=12= 的不同实例]
所以最好声明实例变量或者属性像,
ProgressVC *vc
并只分配一次并从 window 添加或删除此 vc。
我认为这可能会解决您的问题。
试试这个
// 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;
在Swift3.2
let storybord = UIStoryboard.init(name: "Main", bundle: nil)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let vc = storybord.instantiateViewController(withIdentifier: "IDProgressVC")
as IDProgressVC
currentVC = vc.view
appDelegate.window?.addSubview(currentVC!)
// Remove it
currentVC?.removeFromSuperview()
appDelegate.window?.setNeedsLayout()
currentVC = nil
当用户单击添加按钮时,我正在 UIWindow 上添加视图。现在,如果用户再次单击添加按钮,我想先删除该视图并再次添加它。
我已经使用此代码在 UIWindow 上添加视图:
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;
任何帮助将不胜感激。
确保 vc
是同一个对象,没有区别。
我的意思是,当您添加 vc.view 时,您会分配和初始化对象,当您删除时,您会再次分配和初始化,然后两者都是相同 class.[=12= 的不同实例]
所以最好声明实例变量或者属性像,
ProgressVC *vc
并只分配一次并从 window 添加或删除此 vc。
我认为这可能会解决您的问题。
试试这个
// 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;
在Swift3.2
let storybord = UIStoryboard.init(name: "Main", bundle: nil)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let vc = storybord.instantiateViewController(withIdentifier: "IDProgressVC")
as IDProgressVC
currentVC = vc.view
appDelegate.window?.addSubview(currentVC!)
// Remove it
currentVC?.removeFromSuperview()
appDelegate.window?.setNeedsLayout()
currentVC = nil