【发布时间】:2016-02-26 01:04:39
【问题描述】:
我将这个 getCloudKit 函数从 ViewController.swift 移动到 Lay.swift,这样我就可以将所有内容放在一个类中。
var objects = [Lay]()
override func viewDidLoad() {
super.viewDidLoad()
self.refreshControl?.addTarget(self, action: "handleRefresh:", forControlEvents: UIControlEvents.ValueChanged)
self.getCloudKit()
}
func handleRefresh(refreshControl: UIRefreshControl) {
self.objects.removeAll()
self.getCloudKit()
}
func getCloudKit() {
let now = NSDate()
let predicate = NSPredicate(format: "TimeDate > %@", now)
let sort = NSSortDescriptor(key: "TimeDate", ascending: true)
let container = CKContainer.defaultContainer()
let publicData = container.publicCloudDatabase
let query = CKQuery(recordType: “lay”, predicate: predicate)
query.sortDescriptors = [sort]
publicData.performQuery(query, inZoneWithID: nil) { results, error in
if error == nil {
for lay in results! {
let newlay = Lay()
newLay.tColor = lay["tColor"] as! String
newLay.timeDate = lay["TimeDate"] as! AnyObject
newLay.matchup = lay["Matchup"] as! String
let applicationDict = ["tColor" : newLay.tColor, "Matchup" : newLay.matchup]
let transfer = WCSession.defaultSession().transferUserInfo(applicationDict)
self.objects.append(newLay)
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.refreshControl!.endRefreshing()
self.tableView.reloadData()
})
} else {
print(error)
}
}
}
问题是我移动它的时候(以及必要的相关代码):
-
Lay.swift中的错误TableViewController().refreshControl!.endRefreshing()说 “致命错误:在展开时意外发现 nil 可选值” - 需要将我的
WCSession: transferUserInfo代码从getCloudKit放入我的AppDelegate.swift,但尝试时总是出错
新ViewController.swift:
override func viewDidLoad() {
super.viewDidLoad()
self.refreshControl?.addTarget(self, action: "handleRefresh:", forControlEvents: UIControlEvents.ValueChanged)
Lay().getCloudKit()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return Lay().objects.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
let object = Lay().objects[indexPath.row];
if let label = cell.textLabel{
label.text = object.matchup
}
return cell
}
func handleRefresh(refreshControl: UIRefreshControl) {
Lay().objects.removeAll()
Lay().getCloudKit()
}
新Lay.swift:
var objects = [Lay]()
func getCloudKit() {
let now = NSDate()
let predicate = NSPredicate(format: "TimeDate > %@", now)
let sort = NSSortDescriptor(key: "TimeDate", ascending: true)
let container = CKContainer.defaultContainer()
let publicData = container.publicCloudDatabase
let query = CKQuery(recordType: “lay”, predicate: predicate)
query.sortDescriptors = [sort]
publicData.performQuery(query, inZoneWithID: nil) { results, error in
if error == nil {
for lay in results! {
let newlay = Lay()
newLay.tColor = lay["tColor"] as! String
newLay.timeDate = lay["TimeDate"] as! AnyObject
newLay.matchup = lay["Matchup"] as! String
let applicationDict = ["tColor" : newlay.tColor, "Matchup" : newlay.matchup]
let transfer = WCSession.defaultSession().transferUserInfo(applicationDict)
self.objects.append(newlay)
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
TableViewController().refreshControl!.endRefreshing()
TableViewController().tableView.reloadData()
})
} else {
print(error)
}
}
新AppDelegate:
private func setupWatchConnectivity() {
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
private func sendUpdatedDataToWatch(notification: NSNotification) {
if WCSession.isSupported() {
let session = WCSession.defaultSession()
if session.watchAppInstalled
{
let applicationDict = ["TColor" : Lay().tColor, "Matchup" : Lay().matchup]
let transfer = WCSession.defaultSession().transferUserInfo(applicationDict)
NSLog("Transfer AppDelegate: %@", transfer)
NSLog("Trans AppDelegate: %@", applicationDict)
session.transferCurrentComplicationUserInfo(applicationDict)
}
}
}
【问题讨论】:
-
每次您执行
TableViewController()或Lay()时,它都会创建这些类的新实例,而不是访问其他对象正在使用的现有实例。 -
啊,所以我的整个事情都搞砸了,或者我需要做一些小的改变,比如做一个 sharedInstance 什么的?
-
您需要保留对表格视图控制器的引用,可能通过使用 IBOutlet。
-
好的。在
TableViewController中添加IBOutlet,然后在Lay中以某种方式引用它?
标签: ios xcode swift uitableview watchkit