如何将 CloudKit 记录数据从 "TableView-Controller B" 传递到 "View-Controller C"?
How to pass the CloudKit records data from "TableView-Controller B" to "View-Controller C"?
我已经从我的 CloudKit
中获取记录数据并成功显示在 TableViewController-B
上,但是我如何将 CloudKit's records(strings, images)
传递给下一个 ViewController-C
?
ps. not tableViewcontroller-C
我的第一个TableViewController-B
import UIKit
import CloudKit
class BTableViewControoler: UITableViewController {
var myclouds:[CKRecord] = []
var imageCache = NSCache<CKRecordID, NSURL>()
var recordType: String!
override func viewDidLoad() {
super.viewDidLoad()
recordsFromCloud()
}
func recordsFromCloud() {
let cloudContainer = CKContainer.default()
let publicDatabase = cloudContainer.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: recordType, predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = ["name", "address", "GpNumber"]
queryOperation.queuePriority = .veryHigh
queryOperation.resultsLimit = 50
queryOperation.recordFetchedBlock = { (record) -> Void in
self.myclouds.append(record)
}
queryOperation.queryCompletionBlock = { (cursor, error) -> Void in
if let error = error { print("Failed data - \(error.localizedDescription)")
return }
print("Successfully data")
}
publicDatabase.add(queryOperation)
}//fetchRecords End
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myclouds.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MainCell
let newInFo = myclouds[indexPath.row]
cell.mainLabel.text = newInFo.object(forKey: "name") as? String
cell.mainNumber.text = newInFo.object(forKey: "GpNumber") as? String
cell.mainLocation.text = newInFo.object(forKey: "address") as? String
cell.mainImage.image = UIImage(named: "photoalbum")
//get image
if let imageFileURL = imageCache.object(forKey: myclouds.recordID) {
if let imageData = try? Data.init(contentsOf: imageFileURL as URL){
cell.mainImage?.image = UIImage(data: imageData)
}
}else{
let publicDatabase = CKContainer.default().publicCloudDatabase
let fetchRecordsImageOperation = CKFetchRecordsOperation(recordIDs: [newInFo.recordID])
fetchRecordsImageOperation.desiredKeys = ["image"]
fetchRecordsImageOperation.queuePriority = .veryHigh
fetchRecordsImageOperation.perRecordCompletionBlock = {
(record, recordID, error) -> Void in
if let error = error {
print("Failed image:\(error.localizedDescription)")
return
}
if let newinfoRecord = record {
OperationQueue.main.addOperation() {
if let image = newinfoRecord.object(forKey: "image") {
let imageAsset = image as! CKAsset
if let imageData = try? Data.init(contentsOf: imageAsset.fileURL) {
cell.mainImage.image = UIImage(data: imageData)
}
self.imageCache.setObject(imageAsset.fileURL as NSURL, forKey: newInFo.recordID)
}
}
}//record end
}
publicDatabase.add(fetchRecordsImageOperation)
}//else end
return cell
}//cell end
//Segue To Next View
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetailSegue" {
if let indexPath = tableView.indexPathForSelectedRow {
let toDetailVC = segue.destination as! showDetailVC
toDetailVC.DisData = newinfo[indexPath.row]
}
我的ViewController-C
import UIKit
import CloudKit
class showDetailVC: UIViewController {
@IBOutlet weak var detailpic: UIImageView!
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var infoLabel: UILabel!
@IBOutlet weak var mynumberLabel: UILabel!
var DisData: CKRecord?
var getType: String!
override func viewDidLoad() {
super.viewDidLoad()
detailpic.image = UIImage(named: "photoalbum")
infoLabel.text = DisData?.object(forKey: "address") as? String
nameLabel.text = DisData?.object(forKey: "name") as? String
mynumberLable.text = DisData?.object(forKey: "GpNumber") as? String
}
}
我使用 var DisData
从 tableViewController-B
获取 CKRecord
但它不适用于 DisData?.object(forKey: "name") as? String
方法
并且图像也无法弄清楚如何显示它。
我应该再次使用 ViewController-C
中的 UITableViewController-B's
func recordsFromCloud
吗???或任何其他获得 Cloudkit's records
的聪明方法,请帮忙。
创建一个名为 selectedRecord 的变量
var selectedRecord: CKRecord!
添加委托方法didSelectRowatindexPath
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: IndexPath) {
selectedRecord = myclouds[indexPath.row]
performSegueWithIdentifier("showDetailSegue", sender: nil)
}
将 prepareforsegue 更改为此
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetailSegue" {
let toDetailVC = segue.destination as! showDetailVC
toDetailVC.DisData = selectedRecord
}
在 showDetailVC
中将 DisData 的类型从 CKRecord?
更改为 CKRecord!
var DisData: CKRecord!
如果您想拿到钥匙,请在 queryOperation
之外声明 recordsFromCloud()
然后,为showDetailVC
中的Keys创建一个变量
var forKeys: [String]!
并将以下行添加到 prepareforsegue
toDetailVC.forKeys = queryOperation.desiredKeys
我已经从我的 CloudKit
中获取记录数据并成功显示在 TableViewController-B
上,但是我如何将 CloudKit's records(strings, images)
传递给下一个 ViewController-C
?
ps. not tableViewcontroller-C
我的第一个TableViewController-B
import UIKit
import CloudKit
class BTableViewControoler: UITableViewController {
var myclouds:[CKRecord] = []
var imageCache = NSCache<CKRecordID, NSURL>()
var recordType: String!
override func viewDidLoad() {
super.viewDidLoad()
recordsFromCloud()
}
func recordsFromCloud() {
let cloudContainer = CKContainer.default()
let publicDatabase = cloudContainer.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: recordType, predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = ["name", "address", "GpNumber"]
queryOperation.queuePriority = .veryHigh
queryOperation.resultsLimit = 50
queryOperation.recordFetchedBlock = { (record) -> Void in
self.myclouds.append(record)
}
queryOperation.queryCompletionBlock = { (cursor, error) -> Void in
if let error = error { print("Failed data - \(error.localizedDescription)")
return }
print("Successfully data")
}
publicDatabase.add(queryOperation)
}//fetchRecords End
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myclouds.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MainCell
let newInFo = myclouds[indexPath.row]
cell.mainLabel.text = newInFo.object(forKey: "name") as? String
cell.mainNumber.text = newInFo.object(forKey: "GpNumber") as? String
cell.mainLocation.text = newInFo.object(forKey: "address") as? String
cell.mainImage.image = UIImage(named: "photoalbum")
//get image
if let imageFileURL = imageCache.object(forKey: myclouds.recordID) {
if let imageData = try? Data.init(contentsOf: imageFileURL as URL){
cell.mainImage?.image = UIImage(data: imageData)
}
}else{
let publicDatabase = CKContainer.default().publicCloudDatabase
let fetchRecordsImageOperation = CKFetchRecordsOperation(recordIDs: [newInFo.recordID])
fetchRecordsImageOperation.desiredKeys = ["image"]
fetchRecordsImageOperation.queuePriority = .veryHigh
fetchRecordsImageOperation.perRecordCompletionBlock = {
(record, recordID, error) -> Void in
if let error = error {
print("Failed image:\(error.localizedDescription)")
return
}
if let newinfoRecord = record {
OperationQueue.main.addOperation() {
if let image = newinfoRecord.object(forKey: "image") {
let imageAsset = image as! CKAsset
if let imageData = try? Data.init(contentsOf: imageAsset.fileURL) {
cell.mainImage.image = UIImage(data: imageData)
}
self.imageCache.setObject(imageAsset.fileURL as NSURL, forKey: newInFo.recordID)
}
}
}//record end
}
publicDatabase.add(fetchRecordsImageOperation)
}//else end
return cell
}//cell end
//Segue To Next View
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetailSegue" {
if let indexPath = tableView.indexPathForSelectedRow {
let toDetailVC = segue.destination as! showDetailVC
toDetailVC.DisData = newinfo[indexPath.row]
}
我的ViewController-C
import UIKit
import CloudKit
class showDetailVC: UIViewController {
@IBOutlet weak var detailpic: UIImageView!
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var infoLabel: UILabel!
@IBOutlet weak var mynumberLabel: UILabel!
var DisData: CKRecord?
var getType: String!
override func viewDidLoad() {
super.viewDidLoad()
detailpic.image = UIImage(named: "photoalbum")
infoLabel.text = DisData?.object(forKey: "address") as? String
nameLabel.text = DisData?.object(forKey: "name") as? String
mynumberLable.text = DisData?.object(forKey: "GpNumber") as? String
}
}
我使用 var DisData
从 tableViewController-B
获取 CKRecord
但它不适用于 DisData?.object(forKey: "name") as? String
方法
并且图像也无法弄清楚如何显示它。
我应该再次使用 ViewController-C
中的 UITableViewController-B's
func recordsFromCloud
吗???或任何其他获得 Cloudkit's records
的聪明方法,请帮忙。
创建一个名为 selectedRecord 的变量
var selectedRecord: CKRecord!
添加委托方法didSelectRowatindexPath
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: IndexPath) {
selectedRecord = myclouds[indexPath.row]
performSegueWithIdentifier("showDetailSegue", sender: nil)
}
将 prepareforsegue 更改为此
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetailSegue" {
let toDetailVC = segue.destination as! showDetailVC
toDetailVC.DisData = selectedRecord
}
在 showDetailVC
CKRecord?
更改为 CKRecord!
var DisData: CKRecord!
如果您想拿到钥匙,请在 queryOperation
之外声明 recordsFromCloud()
然后,为showDetailVC
var forKeys: [String]!
并将以下行添加到 prepareforsegue
toDetailVC.forKeys = queryOperation.desiredKeys