【问题标题】:Swift - Access elements of the modal popup within UITableViewController in order to pass the data from Firebase database to modal popup elementsSwift - 访问 UITableViewController 中模式弹出窗口的元素,以便将数据从 Firebase 数据库传递到模式弹出窗口元素
【发布时间】:2020-08-13 09:34:01
【问题描述】:

我是 iOS 开发新手,需要您的帮助。

我正在使用 UITableView 来构建任务表。任务数据从 Firebase 数据库传输。当我单击任务单元格上的按钮时,会打开模态弹出窗口(在同一个 ViewController 中通过 animate 方法)。

我想访问模态弹出窗口的元素,以便将特定任务的数据从数据库传递到其模态窗口。

我有包含所有单元格元素出口的 TaskViewCell 文件,例如 previewTitleLabel、previewMotivLabel 等。

TasksListScreen 文件:

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

}}

请帮忙!!

TasksListScreen storyboard with modal popup

Files hierarchy view and TasksListScreen storyboard

App appearance

【问题讨论】:

    标签: ios swift firebase uitableview


    【解决方案1】:

    Sooo,我解决了这个问题。

    为了将所选行的数据传递给新的VC或模态窗口,您需要为表格视图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)!]
            }
     }
    

    【讨论】:

      猜你喜欢
      • 2017-02-11
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 2010-09-05
      相关资源
      最近更新 更多