如何快速弹出和关闭视图5

问题描述 投票:0回答:1

我已经创建了.xib个文件,并将它们与适当的视图控制器相连。不幸的是,进展并不顺利。我已经检查并尝试了许多示例,但不适用于最新版本。使用Xcode 11.3iOS 13。这是我尝试过的代码。TopView.swift

@IBAction func btnConnectTapped(_ sender: Any) {        
        print("tapped")
        var listVC: PopupView    // This one is UIView and popped the window
        listVC = Bundle.main.loadNibNamed("PopupView", owner: self, options: nil)?.first as! PopupView
        self.view.addSubview(listVC)

        // Tried for the UIViewController and not worked
        /*let VC = ScanViewController(nibName: "ScanViewController", bundle: nil)
        //self.present(VC, animated: true, completion: nil)
        self.navigationController?.pushViewController(VC, animated: true)*/
}

Popup.swift //关闭弹出的视图

@IBAction func btnCancelTapped(_ sender: Any) {
        DispatchQueue.main.async {
            self.removeFromSuperview()
        }
}

ScanViewController和Popup视图都有相同的用途。有人可以帮我吗

ios swift swift5 addsubview
1个回答
0
投票

TopView.swift

@IBAction func btnTap(_ sender: UIButton)
   {
    if PopupScreensharedInstance != nil
    {
        PopupScreensharedInstance.makeInstanceNil()
    }
    view.addSubview(popView.SharedInstance())

}

popView.swift

var PopupScreensharedInstance : popView! = nil

class popView: UIView {
    class func SharedInstance() ->popView
    {
        if(PopupScreensharedInstance == nil)
        {
            PopupScreensharedInstance = (Bundle.main.loadNibNamed("popView", owner: self, options: nil)![0] as! popView)
            PopupScreensharedInstance.frame = UIScreen.main.bounds
        }
        return PopupScreensharedInstance
    }

    func makeInstanceNil()
    {
        PopupScreensharedInstance = nil
        self.removeFromSuperview()
    }
    @IBAction func cancle(_ sender: UIButton)
    {
        makeInstanceNil()

    }
}
热门问题
推荐问题
最新问题