我有一个包含许多子视图的父UIView。我需要定期删除子视图并将其从系统中完全删除。这样做的正确方法是什么?我试过这个:
UIView *v = [self.containerView viewWithTag:[n integerValue]];
[v removeFromSuperview];
得到了一个奇怪的结果。以前出现的UIView
也消失了。发生了什么事?
答案 0 :(得分:68)
试试这个:
UIView *v = [self.containerView viewWithTag:[n integerValue]];
v.hidden = YES;
[self.containerView bringSubviewToFront:v];
[v removeFromSuperview];
我刚从UIView类文档中注意到的另一件事 - 见最后一句:
removeFromSuperview 取消接收器与其超视图及其窗口的链接,并将其从响应者链中删除。
讨论 如果接收者的superview不是nil,则此方法释放接收者。如果您计划重用该视图,请确保在调用此方法之前保留该视图,并确保在完成该视图或将其添加到另一个视图层次结构后适当地将其释放。
在显示时不要调用此方法。
更新:现在是2014年,删除子视图而不隐藏它可以很好地完成。原始海报的代码应该按原样运行:
UIView *v = [self.containerView viewWithTag:[n integerValue]];
[v removeFromSuperview];
这将删除v及其作为子视图附加到它的任何视图,留下containerView和v。的任何兄弟。
答案 1 :(得分:38)
要从视图中删除所有子视图:
for(UIView *subview in [view subviews]) {
[subview removeFromSuperview];
}
如果您只想删除某些特定视图:
for(UIView *subview in [view subviews]) {
if([subview isKindOfClass:[UIButton class]]) {
[subview removeFromSuperview];
} else {
// Do nothing - not a UIButton or subclass instance
}
}
您还可以按标记值删除子视图:
for(UIView *subview in [view subviews]) {
if(subview.tag==/*your subview tag value here*/) {
[subview removeFromSuperview];
} else {
// Do nothing - not a UIButton or subclass instance
}
}
答案 2 :(得分:1)
Swift 3.0:
let viewToRemove = mySuperView.viewWithTag(myTag)
viewToRemove?.removeFromSuperview()
答案 3 :(得分:0)
这是正确的总体思路。那些消失的UIViews,他们与这个UIView的关系是什么?他们是这个观点的子视图吗?它们是否在您要移除的视图的dealloc方法中被解除了?
你确定你的标签是唯一的吗?
Sujal
答案 4 :(得分:0)
它们只是从显示屏上消失,还是从显示屏和视图层次结构中消失了?调试器向您展示了什么?
答案 5 :(得分:0)
cell.contentView是否可能与您要删除的子视图具有相同的标记?根据{{3}} viewWithTag删除:
接收者层次结构中的视图 匹配标签。接收器是 包含在搜索中。
如果是这种情况,那么您可能无意中从单元格中删除了cell.contentView。如果n为零且您的单元格的contentview没有设置标记,则默认为0并导致该情况发生。
答案 6 :(得分:0)
快捷键4:扩展UIView
extension UIView {
public func removeAllSubviews() {
for subview in self.subviews {
subview.removeFromSuperview()
}
}
}
或
extension UIView {
public func removeAllSubviews() {
self.subviews.forEach { $0.removeFromSuperview() }
}
}
答案 7 :(得分:0)
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
if let topController = UIApplication.topViewController() {
if topController.isKind(of: ProviderHome.self)
{
let arrOfSuview = self.view.subviews
if arrOfSuview.count > 1
{
print("Davender Arr of subviews : \(arrOfSuview)")
for i in 0..<arrOfSuview.count
{
let objSub = arrOfSuview[i]
if objSub.tag == 101
{
objSub.removeFromSuperview()
}
}
}
NotificationCenter.default.addObserver(self, selector: #selector(ProviderHome.handelPushNotification), name: NSNotification.Name(rawValue: "handelPush"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ProviderHome.handelLocalNotification), name: NSNotification.Name(rawValue: "handelLocal"), object: nil)
}
}
}
@objc func handelPushNotification(_ notification: NSNotification) {
let arrOfSuview = self.view.subviews
if arrOfSuview.count > 1
{
print("Davender Arr of subviews : \(arrOfSuview)")
for i in 0..<arrOfSuview.count
{
let objSub = arrOfSuview[i]
if objSub.tag == 101
{
objSub.removeFromSuperview()
}
}
}
if notification.userInfo != nil
{
let dict = notification.userInfo as! Dictionary<String, Any>
let d = dict["data"] as! Dictionary<String, Any>
let action = d["gcm.notification.label"] as! String
print("current message id :- ", action)
self.getNotificationId = action
if getNotificationId != ""
{
//call the api for getting Data
AppDelegate.sharedInstance().myCurrentnotificationId = getNotificationId
//working code
let storyboard = UIStoryboard(name: "Provider", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "CommonPopUpsVC") as! CommonPopUpsVC
vc.modalPresentationStyle = .overFullScreen
vc.view.frame = self.view.frame
vc.view.tag = 101
self.view.addSubview(vc.view)
self.present(vc, animated: true, completion: nil)
}
}
}