繁体   English   中英

如何快速从UIScrollView内部的超级视图中删除UIView或UIButton

[英]How to remove UIView or UIButton from superview inside UIScrollView in swift

我做了以下代码,将UIView和UIButton添加为UIScrollView中的标签。 一切都很好,可以将数据追加到UIScrollView中。 问题是如何在UIScrollView中删除UIView和UIButton。 我确实删除了,但它并未从UI中消失。 请帮我怎么做。

这是将UIView和UIButton创建为标签

    var xOffset: CGFloat = 0;
    var i = 0;
    for location in locationNameStrs {
        let myString: NSString = location as NSString
        let size: CGSize = myString.sizeWithAttributes([NSFontAttributeName: UIFont(name: "HelveticaNeue", size: 13)!])
        outletView = UIView(frame: CGRectMake(xOffset, 10, size.width + 40, 40))
        outletView.tag = i
        outletStrs = UIButton(frame: CGRectMake(0, 5, outletView.frame.size.width, 25))
        outletStrs.setTitle(location, forState: .Normal)
        outletStrs.titleLabel?.textAlignment = NSTextAlignment.Center
        outletStrs.backgroundColor = UIColor(red:0.00, green:0.45, blue:0.74, alpha:1.0)
        outletStrs.layer.cornerRadius = 12.5
        outletStrs.tag = i
        outletStrs.addTarget(self, action: #selector(self.removeTags(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        outletStrs.titleLabel?.font = UIFont(name: "HelveticaNeue", size: 13)!

        let closeImg = UIImageView (frame: CGRectMake(outletStrs.frame.size.width - 30, 0, 25, 25))
        closeImg.image = UIImage(named: "close")
        closeImg.contentMode = UIViewContentMode.Right
        outletStrs.addSubview(closeImg)
        outletView.addSubview(outletStrs)

        tagScrollView.addSubview(outletView)
        xOffset = xOffset + size.width + 50
        i = i + 1
    }

    tagScrollView.contentSize = CGSizeMake(xOffset + 50, 50)

这里是删除,但问题是那些没有从UI中消失

func removeTags(sender: UIButton) {
    let ok = UIAlertAction(title: "OK", style: .Default) { (action:UIAlertAction!) in
        if self.locationNameStrs.contains(self.locationNameStrs[sender.tag]) {
            self.locationNameStrs.removeAtIndex(sender.tag)
            self.outletView.removeFromSuperview()
            self.outletStrs.removeFromSuperview()
            self.closeImg.removeFromSuperview()
            self.addButton.removeFromSuperview()
            self.displayOutletTags()
        }
    }
}

您没有正确使用警报API。 您忽略了将其显示在屏幕上的基本部分-视图控制器本身。 你需要:

func removeTags(sender: UIButton) {
    let alertBox = UIAlertController(title: "Removing tags", message: "I will now remove some tags.", preferredStyle: .Alert)
    let okAction = UIAlertAction(title: "OK", style: .Default) { (action:UIAlertAction!) in
        guard sender.tag < self.locationNameStrs.count else {
            print("Invalid tag index \(sender.tag)!")
            return
        }
        self.locationNameStrs.removeAtIndex(sender.tag)
        self.outletView.removeFromSuperview()
        self.dislayOutletTags()
    }
    alertBox.addAction(okAction)
    self.presentViewController(alertBox, animated: true, completion: nil)
}

要么

没有警报,因为您似乎不希望这样做:

func removeTags(sender: UIButton) {
    guard sender.tag < self.locationNameStrs.count else {
        print("Invalid tag index \(sender.tag)!")
        return
    }
    self.locationNameStrs.removeAtIndex(sender.tag)
    self.outletView.removeFromSuperview()
    self.dislayOutletTags()
}

您不应遍历滚动视图以删除找到的每个视图。 这是脆弱而笨拙的设计。 您已经在跟踪添加的视图,因此请按名称将其删除。

解决方案中的isKindOfClass()没有意义,因为根据定义,视图的子视图都是UIView所有子类。

同样,您的if语句毫无意义,因为您正在查看数组中的项目,并询问该数组是否包含该项目。

if self.locationNameStrs.contains(self.locationNameStrs[sender.tag]) {
    self.locationNameStrs.removeAtIndex(sender.tag)
    for view in self.tagScrollView.subviews {
        if view.isKindOfClass(UIView) {
            view.removeFromSuperview()
        }
    }
    self.displayOutletTags()
}

以上编码固定了我的要求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM