简体   繁体   English

Swift - 访问 UITableViewController 中模式弹出窗口的元素,以便将数据从 Firebase 数据库传递到模式弹出元素

[英]Swift - Access elements of the modal popup within UITableViewController in order to pass the data from Firebase database to modal popup elements

I'm new to iOS development and need your help.我是 iOS 开发的新手,需要您的帮助。

I'm using UITableView to build a table of tasks.我正在使用 UITableView 来构建任务表。 Tasks data is transmitted from Firebase database.任务数据从 Firebase 数据库传输。 When I click the button on task cell, modal popup window (within the same ViewController via animate method) is opened.当我单击任务单元格上的按钮时,会打开模态弹出窗口 window(在同一个 ViewController 中通过动画方法)。

I want to access elements of modal popup in order to pass the data of the specific task from database to its modal window.我想访问模态弹出窗口的元素,以便将特定任务的数据从数据库传递到其模态 window。

I have TaskViewCell file with all cell elements outlets, such as previewTitleLabel, previewMotivLabel etc.我有包含所有单元格元素出口的 TaskViewCell 文件,例如 previewTitleLabel、previewMotivLabel 等。

TasksListScreen file:任务列表屏幕文件:

import UIKit
import Firebase
import FirebaseAuth
import FirebaseFirestore

class TasksListScreen: UIViewController {
    var db = Firestore.firestore()
    var tasksArray = [Task]() // array of tasks using Task struct

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var centerPopupConstraint: NSLayoutConstraint!
    @IBOutlet weak var backgroundButton: UIButton!

    // show popup  
    @IBAction func yesButtonTapped(_ sender: Any) {
        centerPopupConstraint.constant = 0 // popup appear

        // add slide animation for popup
        UIView.animate(withDuration: 0.3, animations: {
            self.view.layoutIfNeeded()
            self.backgroundButton.alpha = 0.5
        })
    }

    // done button on popup is clicked
    @IBAction func closePopup(_ sender: Any) {
        centerPopupConstraint.constant = -450 // popup disappear

        // add slide animation for popup
        UIView.animate(withDuration: 0.1, animations: {
            self.view.layoutIfNeeded()
            self.backgroundButton.alpha = 0
        })
    }
 }

extension TasksListScreen: UITableViewDataSource, UITableViewDelegate { 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return tasksArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell") as! TaskViewCell
   let task = tasksArray[indexPath.row]

   cell.previewTitleLabel.text = task.title  
   cell.previewMotivLabel.text = task.tip  
   cell.previewHashtagsLabel.text = task.hashtags  

   // Here I want to access task's modal window to pass data to its elements:
   // the same task title and task description form database

}}

Please help!!请帮忙!!

TasksListScreen storyboard with modal popup TasksListScreen storyboard 带模态弹出窗口

Files hierarchy view and TasksListScreen storyboard文件层次结构视图和 TasksListScreen storyboard

App appearance应用外观

Sooo, I solved this issue. Sooo,我解决了这个问题。

In order to pass data of the selected row to new VC or modal window, you need to give an identifier to your segue between table view VC and new VC, and use two functions:为了将所选行的数据传递给新 VC 或模态 window,您需要为表视图 VC 和新 VC 之间的 segue 提供一个标识符,并使用两个函数:

// change VC
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        performSegue(withIdentifier: "showDetails", sender: self)
} 

// pass the data to new VC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? HeroViewController {
            destination.hero = heroes[(tableView.indexPathForSelectedRow?.row)!]
        }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM