【发布时间】:2017-09-13 13:21:24
【问题描述】:
我正在一个类中创建一个自定义委托并尝试在我的控制器中实现该协议。 但是没有作为代表工作是零。 我就是这样做的:
BluetoothOperations.swit
protocol GetWatchCollectedData:NSObjectProtocol {
func getWatchCollectedData(COMMAND_ID : UInt8, data : NSData)
}
class BluetoothOperations: NSObject{
var getWatchCollectedData: GetWatchCollectedData?
func customFunc(){
getWatchCollectedData.getWatchCollectedData(ID,Data)
}
}
SomeController.swift
class SomeController: UIViewController {
let object = BluetoothOperations()
override func viewDidLoad() {
super.viewDidLoad()
object.getWatchCollectedData = self
}
}
extension SomeController: GetWatchCollectedData{
func getWatchCollectedData(COMMAND_ID : UInt8, data : NSData){
print("delegate working.")
}
}
我也尝试过创建一个类对象并在 viewDidLoad 中分配BluetoothOperations's object. getWatchCollectedData = self,但没有成功。
知道我在哪里做错了吗? 谢谢。
【问题讨论】:
-
BluetoothOperations 的对象。 getWatchCollectedData = 自我
-
也试过了。但没有用。 @HarshalValanda
-
你的
BluetoothOperations对象是全局对象吗? ,或者展示你是如何创建实例的 -
让 object = BluetoothOperations(), object.getWatchCollectedData = self
-
@JonSnow 是的,它是全球性的。更新问题。