【问题标题】:How to popup and dismiss a view in swift 5如何在swift 5中弹出和关闭视图
【发布时间】:2020-05-21 02:31:07
【问题描述】:

我创建了 .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 视图具有相同的用途。
谁能帮帮我

【问题讨论】:

  • 您有什么具体问题?您没有为弹出视图设置任何约束。
  • @Sulthan 当我点击按钮删除弹出视图时,它给出了运行时错误。
  • 您可以使用 PopupDialog pod 来显示弹出窗口
  • @SiddhantNigam 你能举个例子吗?
  • 在xib中btnCancelTapped是如何连接的?它是连接到视图本身还是连接到所有者?

标签: ios swift swift5 addsubview


【解决方案1】:

pod 网址:https://github.com/Orderella/PopupDialog

例如制作弹出窗口:

let ratingVC = TimeSelection(nibName: "TimeSelection", bundle: nil)
    ratingVC.view.backgroundColor = .white
    ratingVC.delegate = self
    let popup = PopupDialog(viewController: ratingVC,
                            buttonAlignment: .horizontal,
                            transitionStyle: .bounceDown,
                            tapGestureDismissal: true,
                            panGestureDismissal: true)
    popup.popupContainerView.backgroundColor = .clear
    present(popup, animated: true, completion: nil)

【讨论】:

    【解决方案2】:

    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()
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      相关资源
      最近更新 更多