我做了以下代码,在UIScrollView中添加UIView和UIButton作为标记。一切正常,可以将数据附加到UIScrollView中。问题是如何删除UIView& UIScrollView中的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()
}
}
}
答案 0 :(得分:1)
您没有正确使用警报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()
}
您不应该遍历滚动视图以删除您找到的每个视图。这是脆弱的&amp;尴尬的设计。您已经跟踪添加的视图,因此请按名称删除它。
您的解决方案中的isKindOfClass()
毫无意义,因为根据定义,视图的子视图都是UIView
的子类。
同样,你的if
语句毫无意义,因为你正在查看数组中的一个项目,并询问该数组是否包含它。
答案 1 :(得分:0)
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()
}
以上编码修复了我的请求。