我正在编写一个简单的应用程序来控制我的家庭套件灯。在我的代码中,我尝试将所选设备添加到我的家中,并且它有点工作。但是,在我的应用程序中,当它显示我输入"设置代码"的屏幕时,它会在1-2秒内消失,并说'#34; Home无法配对此配件"在我有机会一直输入设置代码之前。
这是我的代码。
import UIKit
import HomeKit
class HomeTestTableViewController: UITableViewController {
let homeManager = HMHomeManager()
var activeHome: HMHome?
var activeRoom: HMRoom?
var devices = [HMAccessory]()
var selectedAcc: HMAccessory?
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return devices.count
//devices[0].services.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Configure the cell...
let cellIdentifier = "HomeTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! HomeTableViewCell
cell.deviceName.text = devices[indexPath.row].name
//setting button
//let deviceSwitch = UISwitch(frame: CGRect.zero) as UISwitch
return cell
}
@IBAction func unwindToDeviceTable(_ sender: UIStoryboardSegue) {
if let sourceViewController = sender.source as? AddDeviceViewController, let device = sourceViewController.addedAcc{
homeManager.primaryHome?.addAccessory(device, completionHandler: { (error) in
print("ACCESSORY SERVICES: \(device.services.count)")
})
let newIndexPath = IndexPath(row: devices.count, section: 0)
devices.append(device)
tableView.insertRows(at: [newIndexPath], with: .bottom)
tableView.reloadData()
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedAcc = devices[indexPath.row]
//print(selectedAcc?.services.count)
performSegue(withIdentifier: "viewDeviceDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "viewDeviceDetail" {
if let destination = segue.destination as? ShowDeviceViewController {
destination.showSelectedDevice = selectedAcc
}
}
}
}
在我的" UnwindToDeviceTable" segue功能,当我尝试保存配件时,从另一个选择配件的视图控制器。这部分工作,但我不知道为什么设置代码屏幕消失这么快。有人可以帮忙吗? 谢谢!