Ios 如何在Swift中使用委派而不是NSNotification
我正在寻找一种解决方案,以避免在代码中使用NSNotifications。这是我的故事板摘录,用于解释我的问题: 通常涉及两类:Ios 如何在Swift中使用委派而不是NSNotification,ios,swift,delegates,Ios,Swift,Delegates,我正在寻找一种解决方案,以避免在代码中使用NSNotifications。这是我的故事板摘录,用于解释我的问题: 通常涉及两类: import UIKit class ScanTableView: UITableViewController { @IBOutlet var ScanTableView: UITableView! var valueCollectionView: ValueCollectionView? override func viewDidLo
import UIKit
class ScanTableView: UITableViewController
{
@IBOutlet var ScanTableView: UITableView!
var valueCollectionView: ValueCollectionView?
override func viewDidLoad()
{
super.viewDidLoad()
self.refreshControl!.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return BluetoothManager.bluetoothManager.peripheralArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = ScanTableView.dequeueReusableCellWithIdentifier("scanCell",forIndexPath: indexPath)
cell.textLabel!.text = BluetoothManager.bluetoothManager.peripheralArray[indexPath.row].name
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
BluetoothManager.bluetoothManager.connectPeripheral(indexPath.row)
}
func refresh()
{
BluetoothManager.bluetoothManager = BluetoothManager()
}
}
import UIKit
class ValueCollectionView: UICollectionViewController
{
@IBOutlet var ValueCollectionView: UICollectionView!
var scanTableView: ScanTableView?
var valueCollectionViewCell: ValueCollectionViewCell = ValueCollectionViewCell()
override func viewDidLoad()
{
super.viewDidLoad()
BluetoothManager.bluetoothManager.valueCollectionView = ValueCollectionView
self.navigationItem.hidesBackButton = true
let disconnectButton = UIBarButtonItem(title: "Disconnect", style: UIBarButtonItemStyle.Plain, target: self, action: "disconnect:")
self.navigationItem.leftBarButtonItem = disconnectButton;
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
//Function to go back to the ScanTableView
//The ScanTableView should be clear after the reload
func disconnect(sender: UIBarButtonItem)
{
BluetoothManager.bluetoothManager.disconnectPeripheral(BluetoothManager.bluetoothManager.selectedPeripheralIndex!)
//Remove all Items of the TableView
BluetoothManager.bluetoothManager.peripheralArray.removeAll()
let switchViewController = self.navigationController?.viewControllers[1] as! ScanTableView
self.navigationController?.popToViewController(switchViewController, animated: true)
// This part doesn't work! The appearing TableView isn't cleared
scanTableView?.ScanTableView.reloadData()
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return BluetoothManager.bluetoothManager.measurementValue.count
}
override func collectionView(tableView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell: ValueCollectionViewCell = collectionView?.dequeueReusableCellWithReuseIdentifier("valueCell", forIndexPath: indexPath) as! ValueCollectionViewCell
cell.ValueCollectionViewLabel.text = BluetoothManager.bluetoothManager.measurementValue[indexPath.row]
return cell
}
}
ScanTableView
\ValueCollectionView
//BluetoothManager类对我的问题不感兴趣,工作正常
扫描表视图类:
import UIKit
class ScanTableView: UITableViewController
{
@IBOutlet var ScanTableView: UITableView!
var valueCollectionView: ValueCollectionView?
override func viewDidLoad()
{
super.viewDidLoad()
self.refreshControl!.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return BluetoothManager.bluetoothManager.peripheralArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = ScanTableView.dequeueReusableCellWithIdentifier("scanCell",forIndexPath: indexPath)
cell.textLabel!.text = BluetoothManager.bluetoothManager.peripheralArray[indexPath.row].name
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
BluetoothManager.bluetoothManager.connectPeripheral(indexPath.row)
}
func refresh()
{
BluetoothManager.bluetoothManager = BluetoothManager()
}
}
import UIKit
class ValueCollectionView: UICollectionViewController
{
@IBOutlet var ValueCollectionView: UICollectionView!
var scanTableView: ScanTableView?
var valueCollectionViewCell: ValueCollectionViewCell = ValueCollectionViewCell()
override func viewDidLoad()
{
super.viewDidLoad()
BluetoothManager.bluetoothManager.valueCollectionView = ValueCollectionView
self.navigationItem.hidesBackButton = true
let disconnectButton = UIBarButtonItem(title: "Disconnect", style: UIBarButtonItemStyle.Plain, target: self, action: "disconnect:")
self.navigationItem.leftBarButtonItem = disconnectButton;
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
//Function to go back to the ScanTableView
//The ScanTableView should be clear after the reload
func disconnect(sender: UIBarButtonItem)
{
BluetoothManager.bluetoothManager.disconnectPeripheral(BluetoothManager.bluetoothManager.selectedPeripheralIndex!)
//Remove all Items of the TableView
BluetoothManager.bluetoothManager.peripheralArray.removeAll()
let switchViewController = self.navigationController?.viewControllers[1] as! ScanTableView
self.navigationController?.popToViewController(switchViewController, animated: true)
// This part doesn't work! The appearing TableView isn't cleared
scanTableView?.ScanTableView.reloadData()
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return BluetoothManager.bluetoothManager.measurementValue.count
}
override func collectionView(tableView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell: ValueCollectionViewCell = collectionView?.dequeueReusableCellWithReuseIdentifier("valueCell", forIndexPath: indexPath) as! ValueCollectionViewCell
cell.ValueCollectionViewLabel.text = BluetoothManager.bluetoothManager.measurementValue[indexPath.row]
return cell
}
}
ValueCollectionView类:
import UIKit
class ScanTableView: UITableViewController
{
@IBOutlet var ScanTableView: UITableView!
var valueCollectionView: ValueCollectionView?
override func viewDidLoad()
{
super.viewDidLoad()
self.refreshControl!.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return BluetoothManager.bluetoothManager.peripheralArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = ScanTableView.dequeueReusableCellWithIdentifier("scanCell",forIndexPath: indexPath)
cell.textLabel!.text = BluetoothManager.bluetoothManager.peripheralArray[indexPath.row].name
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
BluetoothManager.bluetoothManager.connectPeripheral(indexPath.row)
}
func refresh()
{
BluetoothManager.bluetoothManager = BluetoothManager()
}
}
import UIKit
class ValueCollectionView: UICollectionViewController
{
@IBOutlet var ValueCollectionView: UICollectionView!
var scanTableView: ScanTableView?
var valueCollectionViewCell: ValueCollectionViewCell = ValueCollectionViewCell()
override func viewDidLoad()
{
super.viewDidLoad()
BluetoothManager.bluetoothManager.valueCollectionView = ValueCollectionView
self.navigationItem.hidesBackButton = true
let disconnectButton = UIBarButtonItem(title: "Disconnect", style: UIBarButtonItemStyle.Plain, target: self, action: "disconnect:")
self.navigationItem.leftBarButtonItem = disconnectButton;
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
//Function to go back to the ScanTableView
//The ScanTableView should be clear after the reload
func disconnect(sender: UIBarButtonItem)
{
BluetoothManager.bluetoothManager.disconnectPeripheral(BluetoothManager.bluetoothManager.selectedPeripheralIndex!)
//Remove all Items of the TableView
BluetoothManager.bluetoothManager.peripheralArray.removeAll()
let switchViewController = self.navigationController?.viewControllers[1] as! ScanTableView
self.navigationController?.popToViewController(switchViewController, animated: true)
// This part doesn't work! The appearing TableView isn't cleared
scanTableView?.ScanTableView.reloadData()
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return BluetoothManager.bluetoothManager.measurementValue.count
}
override func collectionView(tableView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell: ValueCollectionViewCell = collectionView?.dequeueReusableCellWithReuseIdentifier("valueCell", forIndexPath: indexPath) as! ValueCollectionViewCell
cell.ValueCollectionViewLabel.text = BluetoothManager.bluetoothManager.measurementValue[indexPath.row]
return cell
}
}
问题:
import UIKit
class ScanTableView: UITableViewController
{
@IBOutlet var ScanTableView: UITableView!
var valueCollectionView: ValueCollectionView?
override func viewDidLoad()
{
super.viewDidLoad()
self.refreshControl!.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return BluetoothManager.bluetoothManager.peripheralArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = ScanTableView.dequeueReusableCellWithIdentifier("scanCell",forIndexPath: indexPath)
cell.textLabel!.text = BluetoothManager.bluetoothManager.peripheralArray[indexPath.row].name
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
BluetoothManager.bluetoothManager.connectPeripheral(indexPath.row)
}
func refresh()
{
BluetoothManager.bluetoothManager = BluetoothManager()
}
}
import UIKit
class ValueCollectionView: UICollectionViewController
{
@IBOutlet var ValueCollectionView: UICollectionView!
var scanTableView: ScanTableView?
var valueCollectionViewCell: ValueCollectionViewCell = ValueCollectionViewCell()
override func viewDidLoad()
{
super.viewDidLoad()
BluetoothManager.bluetoothManager.valueCollectionView = ValueCollectionView
self.navigationItem.hidesBackButton = true
let disconnectButton = UIBarButtonItem(title: "Disconnect", style: UIBarButtonItemStyle.Plain, target: self, action: "disconnect:")
self.navigationItem.leftBarButtonItem = disconnectButton;
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
//Function to go back to the ScanTableView
//The ScanTableView should be clear after the reload
func disconnect(sender: UIBarButtonItem)
{
BluetoothManager.bluetoothManager.disconnectPeripheral(BluetoothManager.bluetoothManager.selectedPeripheralIndex!)
//Remove all Items of the TableView
BluetoothManager.bluetoothManager.peripheralArray.removeAll()
let switchViewController = self.navigationController?.viewControllers[1] as! ScanTableView
self.navigationController?.popToViewController(switchViewController, animated: true)
// This part doesn't work! The appearing TableView isn't cleared
scanTableView?.ScanTableView.reloadData()
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return BluetoothManager.bluetoothManager.measurementValue.count
}
override func collectionView(tableView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell: ValueCollectionViewCell = collectionView?.dequeueReusableCellWithReuseIdentifier("valueCell", forIndexPath: indexPath) as! ValueCollectionViewCell
cell.ValueCollectionViewLabel.text = BluetoothManager.bluetoothManager.measurementValue[indexPath.row]
return cell
}
}
为什么我无法在标记的部分重新加载ScanTableView?通话后没有任何事情发生:
scanTableView?.ScanTableView.reloadData()
在代码中,任何时候都没有将任何对象设置为ValueCollectionView的
scanTableView
。因此,它是nil
,并且reloadData()
行没有效果
换句话说,如果要使用委托,必须有一个委托。为什么要显示所有代码,问题是什么?只是你不知道什么是授权,你想知道吗?现在更清楚了吗?好的,我怎么解决这个问题?我试图将“valueCollectionView?.scanTableView=scanTableView”添加到我的scanTableView的viewDidLoad函数中。但我总是会遇到错误。错误是:“无法将“UITableView!”类型的值分配给“ScanTableView”类型的值。”“ScanTableView是视图控制器类型,而不是表视图类型。委托必须是ScanTableView本身。