IOS Swift3如何随机删除自定义UIView

时间:2017-05-26 05:18:06

标签: ios swift uiview

我正在swift 3中构建一个iOS应用程序,我正在创建动态UIViews。我需要随机删除自定义视图。请帮帮我,我坚持了很久。 提前致谢

class ViewController: UIViewController { 

    var myView: subView! 
    var y : CGFloat! 
    @IBOutlet weak var addButton: UIButton!

    override func viewDidLoad() {
        y = 1
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    func cancelbutton(_ sender: UIButton) {
        myView.removeFromSuperview()
    }

    @IBAction func buttonAction(_ sender: Any) {
        y = y + 110
        myView = subView(frame: CGRect(x: 80, y: y, width: 300, height: 100))

        myView.backgroundColor = UIColor.green
        myView.actionButton.addTarget(self, action: (#selector(cancelbutton(_:))), for: UIControlEvents.touchUpInside)
        self.view.addSubview(myView)
    }
}

enter image description here

enter image description here

enter image description here

正如您在上面的图片中看到的,当我点击关闭时,SubView(用户视图)关闭,但是绿色的MyView不会停留在那里。有人请帮助.........

required init?(coder aDecoder: NSCoder)
   {
       super.init(coder: aDecoder)
       Bundle.main.loadNibNamed("subView",owner: self, options:nil)
       self.addSubview(self.views)
       Bundle.main.loadNibNamed("subView",owner: self, options:nil)
       self.addSubview(self.views)

   }
   override init(frame: CGRect)
   {
       super.init(frame: frame)
       Bundle.main.loadNibNamed("subView", owner: self, options: nil)
       views.frame = bounds
       self.addSubview(self.views)
   }

   @IBAction func buttonAction(_ sender: Any) {
       views.removeFromSuperview()

   }

3 个答案:

答案 0 :(得分:2)

你做错的是你添加了多个subView视图,你选择的不删除的原因。请修改您的代码,如下所示。 我所做的是,无论何时添加新的subView,您还将设置其标记值,当您选择视图以删除其标记值并在该标记值上放置if语句时。

class ViewController: UIViewController { 
    var myView: subView! 
    var y : CGFloat! 
    var tag : Int = 0 
    @IBOutlet weak var addButton: UIButton!

    override func viewDidLoad() {
        y = 1
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    func cancelbutton(_ sender: UIButton)
    {
                let selectViewTagValue : Int = sender.tag /// save the selected view tag value
                for object in self.view.subviews {

                if ((object is subView) && object.tag == selectViewTagValue)
                {
                    object.removeFromSuperview()
                }
            }
    }
    @IBAction func buttonAction(_ sender: Any) {
        y = y + 110
        myView = subView(frame: CGRect(x: 80, y: y, width: 300, height: 100))
        myView.tag = tag 
        myView.actionButton.tag = tag
        tag = tag + 1
        myView.backgroundColor = UIColor.green
        myView.actionButton.addTarget(self, action: (#selector(cancelbutton(_:))), for: UIControlEvents.touchUpInside)
        self.view.addSubview(myView)

    }

答案 1 :(得分:1)

我设法修复了删除问题,但目前我无法重新定位海关查看的位置,如下图所示,请帮助我。

enter image description here enter image description here

答案 2 :(得分:0)

尝试更改取消操作,如下所示:

func cancelbutton(_ sender: UIButton)
{
    if let myView = sender.superview {
        myView.removeFromSuperview()
    }
}

只需删除按钮的容器视图,而不是全局myView。