当用户导航到另一个视图控制器而不关闭当前弹出视图时,弹出视图控制器显示深色背景

时间:2018-03-11 11:56:59

标签: ios uitableview uiviewcontroller popup uitabbarcontroller

我有一个表格视图,它位于标签栏内。当我单击表视图时,它会显示一个弹出式视图控制器,如下所示:

enter image description here

但是,每当我导航到另一个标签栏时,保持弹出视图不会关闭它,当我回到上一个弹出视图时,它会显示一个深色背景,如下图所示: / p>

enter image description here

我该如何解决这个问题?

*注意=我是IOS的新手

添加子视图的代码:

import UIKit

class NotificationViewController:   UIViewController,UITableViewDelegate,UITableViewDataSource {

var dataALL = [NotificationDataModel]()

@IBOutlet weak var tableViewAll: UITableView!

@IBOutlet  var mainSegmentedControl: UISegmentedControl!

lazy var iNotificationViewController : INotificaitonViewController = {

    let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
    var viewController = storyBoard.instantiateViewController(withIdentifier: "INotificaitonViewController") as! INotificaitonViewController

    self.addViewContollerAsChildViewcontroller(childViewContoller: viewController)
    return viewController

}()

var activityIndicator : UIActivityIndicatorView = UIActivityIndicatorView()


override func viewDidLoad() {
    super.viewDidLoad()
    tableViewAll.delegate = self
    tableViewAll.dataSource = self
    let nib  = UINib(nibName: "TableViewCell", bundle: nil)
    tableViewAll.register(nib, forCellReuseIdentifier: "customCell")
    setupView()
    tableViewAll.separatorStyle = .none
    self.startShowingIndication()
    downloadAllJSONData {
        self.tableViewAll.reloadData()
        self.stopShowingIndication()
        print("sucess")

    }
}

func downloadAllJSONData(completed : @escaping ()->()){
    guard let url = URL(string: "http://www.swipecrafts.com/notice/get")else {return}
    var request = URLRequest.init(url: url)
    request.httpMethod = "POST"
    request.addValue("cf7ab8c9d4efae82b575eabd6bec76cbb86c6108391e036387f3d5356a582171519367747000", forHTTPHeaderField: "api_ky")
    let postdata = "school_id=\(SCHOOL_ID)"
    request.httpBody = postdata.data(using: .utf8)

    URLSession.shared.dataTask(with: request) { (data, response, error) in
        if error == nil{
            do{
                self.dataALL = try JSONDecoder().decode([NotificationDataModel].self, from: data!)
                DispatchQueue.main.async {
                    completed()
                    print(self.dataALL.count)
                }
            }catch{
                print(error)
            }
        }
    }.resume()
}

private func setupView(){
    setupSegmentedControl()
    updateView()

}
private func updateView(){
    iNotificationViewController.view.isHidden = !(mainSegmentedControl.selectedSegmentIndex == 0)



}
private func setupSegmentedControl(){
    mainSegmentedControl.removeAllSegments()
    mainSegmentedControl.insertSegment(withTitle: "General Notification", at: 0, animated: false)
    mainSegmentedControl.insertSegment(withTitle: "All Notificatoin", at: 1, animated: false)
    mainSegmentedControl.addTarget(self, action: #selector(selectionDidChange(sender:)), for: .valueChanged)

    mainSegmentedControl.selectedSegmentIndex = 0

}
@objc func selectionDidChange(sender: UISegmentedControl)  {
    updateView()
}

private func addViewContollerAsChildViewcontroller (childViewContoller : UIViewController){
    addChildViewController(childViewContoller)
    view.addSubview(childViewContoller.view)

    childViewContoller.view.frame = view.bounds
    childViewContoller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    childViewContoller.didMove(toParentViewController: self)


}
private func removeViewControllerAsChildViewController(childViewController : UIViewController){
    childViewController.willMove(toParentViewController: nil)
    childViewController.view.removeFromSuperview()
    childViewController.removeFromParentViewController()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dataALL.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableViewAll.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! TableViewCell
    cell.customInit(noticeTitle: dataALL[indexPath.row].notice_title!,noticeDiscripetion: dataALL[indexPath.row].notice_description!)
    let downloadedimage = dataALL[indexPath.row].image_name
    if downloadedimage != nil{
        cell.lblImageNotificaiton.downloadedFrom(link: downloadedimage!)
    }
    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "goToPopUpViewww", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? PopUpViewController{
        destination.notificationfianlData = dataALL[(tableViewAll.indexPathForSelectedRow?.row)!]
    }

}
func startShowingIndication(){

    activityIndicator.center = self.view.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
    view.addSubview(activityIndicator)

    activityIndicator.startAnimating()


}


func stopShowingIndication(){
    activityIndicator.stopAnimating()


}


}

4 个答案:

答案 0 :(得分:0)

我会调查UIPopoverPresentationController

通常,当您以您所描述的方式呈现某些内容时,如果没有先解除" popup"那么用户就无法离开父视图控制器。或" popover"视图。因此,即使您不使用UIPopoverPresentationController,也应考虑通过其外部的任何点击来解除弹出视图。

更新4/3/18

我肯定会建议研究和阅读一些文档或者阅读一些教程,以便您可以准确理解代码的作用和原因。如果您还没有我上面提到的UIPopoverPresentationController的文档链接,请指向this,这正是您所需要的。

以下是您的部分代码示例。

lazy var iNotificationViewController : INotificaitonViewController = {
    let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let viewController = storyBoard.instantiateViewController(withIdentifier: "INotificaitonViewController")
    // may want to set the viewController.preferredContentSize here => which will be the size of your popover
    viewController.modalPresentationStyle = .popover

    if let popoverPresentationController = viewController.popoverPresentationController {
        popoverPresentationController.delegate = self
        // set the .sourceView and .sourceRect so that the popover can position itself and arrow accordingly
    }


    present(viewController, animated: true) {
        // closure for when the popover is visible
    }
    return viewController
}

正如@Britto Thomas所提到的,你真的不应该在小时候添加你的popover而是展示它。

更新4/9/18

道歉,我忘了添加这个tid位,这非常重要。由于弹出窗口适用于iPad,您必须按如下方式实现此委托方法:

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
}

否则它会简单地将其呈现为任何其他模态,尽管事实上我们告诉了'modalPresentationStyle'要成为' .popover'

答案 1 :(得分:0)

目前,您正在NotificationViewController的上下文中展示popupviewcontroller。使用Perform Segue“goToPopUpViewww”。

请尝试在UITabBarController的上下文中显示popupviewcontroller,而不是那样。这更有意义。这种变化情况为 - 当弹出窗口时,用户无法切换选项卡。

答案 2 :(得分:0)

因此,您可以尝试以编程方式或通过故事板更改演示文稿样式(例如,当前上下文),这可能有助于此方案。 请参见屏幕截图enter image description here

答案 3 :(得分:0)

这是Apple文档中转换为Swift 3的示例项目。它使用自定义UIPresentationController来模态呈现ViewControllers,并控制它们在屏幕上和屏幕外的动画效果。按照相同的方法,你不应该有任何深色背景问题。

https://github.com/ooper-shlab/CustomTransitions-Swift