简体   繁体   English

如何从另一个类访问一个类中的值? 斯威夫特4

[英]How to access values in one class from another? Swift 4

I have an app where when a user taps a cell from a TableView representing a group of images, he is taken to another tableView where all the images within that group should be shown. 我有一个应用程序,当用户从代表一组图像的TableView点击一个cell ,他被带到另一个tableView,其中应显示该组中的所有图像。 However I am unsure of how to do this. 但是,我不确定如何执行此操作。

I have currently have extracted at the beginning the info necessary to make a reference and get all the data, which is in an array of objects. 我目前已经在开始时提取了必要的信息,以供参考并获取所有数据,这些数据位于对象数组中。 However how can I access these values from another class? 但是,如何从另一个类访问这些值?

DataModel: DataModel中:

struct UserImage {
    var userID: String
    var image: UIImage
    var postNum: String
}

I am creating an array of this as shown bellow, in a P1TableVC : 我在P1TableVC创建如下所示的数组:

let arrayOfUserImageData = [UserImage]()

The function which retrieves and store the data looks as follows: 检索和存储数据的函数如下所示:

func fetchAllUserFristImage() {
print("Description: calling of fetchAllUserFristImage()")
Database.database().reference().child("Posts").observe(.childAdded, with: {(snapshot) in

    if snapshot.value as? [String: AnyObject] != nil {
        let user = snapshot.key
        print("Description: calling of snapshot.value is not nil ")

        self.databaseRef = Database.database().reference()

        let usersPostRef2 = self.databaseRef.child("Posts").child(user)

        usersPostRef2.observe(.value, with: {(postXSnapshots) in

            if let postDictionary2 = postXSnapshots.value as? [String:AnyObject] {

                for (p) in postDictionary2 {

                    if let posts = p.value as? [String:AnyObject] {
                        print("Description: posts has value of:  \(posts)")

                        //to get back to where i was delete the below for i
                        for (i) in posts {

                            if let imageUrlString = i.value as? [String:AnyObject], let postUrl = imageUrlString["image1"] as? String {
                                print("Description: inside the if let imageUrlString = i.value ")

                                self.feedArray.append(Post(fetchedImageURL: postUrl))
                                if let imageUrl = URL(string: postUrl), let imageDataL = try? Data(contentsOf: imageUrl), let image = UIImage(data: imageDataL) {
                                    print("Description: inside the if let imageUrl = URL(string: postUrl)")
                                    print("Description: img url's of posts: \(imageUrl)")
                                    self.tableData.append(UserImage(userID: user, image: image, postNum: p.key))
                                    self.tableView.reloadData()
                                } else {print("this user had no posts, was nil")}
                            }
                        }
                    }
                }
            }
        })
        //below shud stay same
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }
})
}

Or you may probably use Singleton 或者您可能使用Singleton

For instance: just create class Model 例如:只需创建类Model

class Model {
    static let sharedInstance = Model()
    var tableVC: P1TableVC!
}

Then inside of your VC in viewDidLoad 然后在VC中的viewDidLoad中

class P1TableVC: UIViewController {
    override func viewDidLoad() {
        Model.sharedInstance.tableVC = self
    }
}

Then you can use your class everywhere you want 然后,您可以在任何地方使用您的课程

Model.sharedInstance.tableVC.arrayOfUserImageData

Or you can create variable for all your data 或者您可以为所有数据创建变量

class Model {
    static let sharedInstance = Model()
    var data: [UserImage]()
}

And then use data not VC 然后用数据不VC

Model.sharedInstance.data

In didSelectItem method fetch the selected data from tableData based on indexPath.row and pass this data to next controller (make a variable in next controller and before pushing/presenting assign the fetched data to that variable). didSelectItem方法取从所选择的数据tableData基于indexPath.row并传递该数据到下一个控制器(使下一个控制器和推前一个可变/呈现分配取出的数据到该变量)。

  1. didSelectItem : Fetch the selected data - let data = tableData[indexPath.row] didSelectItem :获取所选数据let data = tableData[indexPath.row]
  2. Assign it to next controller's variable - 将其分配给下一个控制器的变量-

    let vc = nextVC() vc.fetchedData = data push/present vc

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

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