更新/刷新ContainerView的内容

时间:2018-06-27 04:58:09

标签: ios swift uicontainerview

我目前有一个视图控制器,该控制器具有一个容器视图,其功能类似于iOS Maps应用程序中的底部抽屉。扫描图像后,抽屉会动画显示在屏幕上(从(0,700)开始隐藏在屏幕上)并显示来自Firebase的信息。在此容器中,我有另一个容器,可让我进行PageViewController设置。此设置允许用户在2套不同的信息集之间滑动(所有信息都需要从Firebase加载)。

  

我不知道如何在第二个PageViewController中重置数据,以便每当通过按钮关闭抽屉时,PageViewController系统就会重置,并且我可以在secondPageViewController内部使用我的函数来重新加载其内部的tableView。 / p>

TL; DR /更多信息

  • 扫描图像会将信息加载到ViewController中。

  • 我有一个独立的VC(ViewController),其中有一个容器视图(BottomDrawerViewController)。 BottomDrawerViewController内部还具有一个容器,该容器是一个包含2页的PageViewController(Swipe1ViewController和Swipe2ViewController)

  • PageViewController设置的第一页工作正常(很可能是因为该信息是使用NSNotification加载的)

  • 我无法以相同的方式使用NSNotification发送和刷新此数据,因为这不是我发布通知后显示的第一个视图控制器。

  • 我不能只将我的所有信息单独加载到第二个PageViewController中,因为我从原始ViewController中获得了Firebase信息的路径

问题:如何重置此数据和PageViewController,以便每当用户关闭底部抽屉时,第二页上的信息就可以在下一次扫描时刷新,而PageViewController从第一页开始?我想也许可以通过强迫视图消失来让ViewDidLoad在屏幕上出现时被调用,但我意识到这是不可能的。

我附上了所有视图控制器中的一些重要代码以及图像和JSON形式的Firebase代码,以防万一。

ViewController:-这是获取信息以发送到容器以及在主容器中进行动画处理的部分

   ref.child("Directory").child(imageName).observeSingleEvent(of: .value, with: { (snapshot) in
            print(snapshot)
            let content = ImageInformation(snapshot: snapshot)

            // TODO: Deliver Image Metadata here
            let imageInfo = ["eventTitle": imageName, "source": content.Source, "date": content.Date] as [String : Any]
            NotificationCenter.default.post(name: Notification.Name.drawerVC, object: nil, userInfo: imageInfo)
            NotificationCenter.default.post(name: Notification.Name.newsVC, object: nil, userInfo: imageInfo)
          })

        UIView.animate(withDuration: 0.50) {
        self.bottomDrawer.frame = CGRect(x: self.bottomPosition.x, y: self.bottomPosition.y, width: self.bottomDrawer.frame.size.width, height: self.bottomDrawer.frame.size.height)
        }

BottomDrawerViewController::-这会从ViewController收到通知并更改抽屉标题。当视图被按下时,它还会向ViewController发送通知,以便将其关闭。

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(appeared), name: Notification.Name.drawerVC, object: nil)

      let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))

        view.addGestureRecognizer(tap)
    } 
@objc func appeared(_ notification: NSNotification) {

        if let eventTitle = notification.userInfo?["eventTitle"] as? String {
            self.eventTitleLabel.text = eventTitle
        }
    }
    @objc func handleTap(_ sender: UITapGestureRecognizer) {
         NotificationCenter.default.post(name: Notification.Name.mainVC, object: nil, userInfo: nil)
    }

PageViewController:-这控制了我的PageViewController设置。由于所有内容都与我未包含的常规设置相同。此行允许我将变量从第一页发送到第二页。

var eventTitle: String = ""

Swipe1ViewController::-从ViewController接收通知,并使用它来填充屏幕上的标签。还将事件标题(数据库中的“测试输入”)发送到PageViewController,以供Swipe2ViewController使用(未显示代码)

Swipe2ViewController:-截至目前,它从PageViewController中获取eventTitle变量,并使用它从Firebase中获取数据,但这不能多次使用,因为viewDidLoad()仅在以下情况下运行一次BottomDrawerViewController向上滑动。我还尝试将fetchHeadlines()函数放入viewDidAppear()中,但是如果用户关闭并扫描了完全不同的图片,它将不会更新。

override func viewDidLoad() {
        super.viewDidLoad()
        self.newsTableView.delegate = self
        self.newsTableView.dataSource = self

       // fetchHeadlines()
    }

    override func viewDidAppear(_ animated: Bool) {
        //
        fetchHeadlines()
    }

    override func viewDidDisappear(_ animated: Bool) {
    }

    func fetchHeadlines() {
        let pageVC = self.parent as! DrawerPageViewController
        let eventTitle = pageVC.eventTitle
        print("Title: " + eventTitle)
        ref.child("Directory").child(eventTitle).child("News").observeSingleEvent(of: .value) { (newsSnapshot) in
            for news in newsSnapshot.children.allObjects as! [DataSnapshot] {
                print("Key: \(news.key)", "Value: \(news.value)")
                //self.headlines.append(news)
                DispatchQueue.global().async {
                    DispatchQueue.main.async {
                       // self.tableView.reloadData()
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您不应使用通知将数据从一个视图传播到另一个视图。如您所见,它不能很好地工作,容易出错,调试起来也很困难。相反,您应该接受父视图控制器和子视图控制器之间的关系。也就是说,ViewController应该将数据传递给BottomViewController,后者将数据传递给PageViewController,这是PageViewController的任务是在子页面中设置数据。这样,当每个视图控制器放在屏幕上时,您就可以将数据放在其所属位置。

请注意,父视图控制器可以使用// original data table DataTable origDT; // create a reader DataReader dr = origDT.CreatDataReader(); //clone original DataTable clonedDT = origDT.Clone(); //add identity column clonedDT.Columns.Add(new DataColumn(){AutoIncrement=true}); //load clone from reader, identity col will auto-populate with values clonedDT.Load(dr); 数组访问子级。因此,ViewController可以使用以下命令直接访问BottomViewController:

childViewControllers