Ios 使用自定义委托初始化
我正在类中创建一个自定义委托,并尝试在控制器中实现该协议。 但不作为代表工作是零。 我就是这样做的: 蓝牙操作。switIos 使用自定义委托初始化,ios,swift,delegates,Ios,Swift,Delegates,我正在类中创建一个自定义委托,并尝试在控制器中实现该协议。 但不作为代表工作是零。 我就是这样做的: 蓝牙操作。swit protocol GetWatchCollectedData:NSObjectProtocol { func getWatchCollectedData(COMMAND_ID : UInt8, data : NSData) } class BluetoothOperations: NSObject{ var getWatchCollectedData: Ge
protocol GetWatchCollectedData:NSObjectProtocol {
func getWatchCollectedData(COMMAND_ID : UInt8, data : NSData)
}
class BluetoothOperations: NSObject{
var getWatchCollectedData: GetWatchCollectedData?
func customFunc(){
getWatchCollectedData.getWatchCollectedData(ID,Data)
}
}
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.")
}
}
SomeController.swift
protocol GetWatchCollectedData:NSObjectProtocol {
func getWatchCollectedData(COMMAND_ID : UInt8, data : NSData)
}
class BluetoothOperations: NSObject{
var getWatchCollectedData: GetWatchCollectedData?
func customFunc(){
getWatchCollectedData.getWatchCollectedData(ID,Data)
}
}
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.")
}
}
我还尝试创建一个类对象并分配
BluetoothOperations的对象。getWatchCollectedData=self
在viewDidLoad中,但不起作用
知道我哪里做错了吗?
谢谢。您的代码看起来正确。这就是它可能失败的原因。在
SomeController
中,您正在从BluetoothOperations
类创建对象
,并正确分配了委托
但是,必须确保控制器中定义的对象正在发送委托调用。您可能正在使用另一个BluetoothOperations
实例,但在该实例中,未设置委托
检查这一点的最简单方法是在视图控制器中创建的对象中手动触发委托。请尝试以下代码以查看代理是否被触发
class SomeController: UIViewController {
let object = BluetoothOperations()
override func viewDidLoad() {
super.viewDidLoad()
object.getWatchCollectedData = self
object.customFunc()// Trigger the delegate function here
}
}
extension SomeController: GetWatchCollectedData{
func getWatchCollectedData(COMMAND_ID : UInt8, data : NSData){
print("delegate working.")
}
}
您已经正确地实现了委托,但没有在适当的类之间实现 当您在
SomeController
中以及在其他视图controller中创建BluetoothOperations
对象时,让我们假设SomeController1
现在,代理只负责相关的类
像
你就这样试着
`SomeController1 ---> `BluetoothOperations` (Call `customFunc`) ----> SomeController`
这不可能通过这种方式实现
因为您的SomeController1
和SomeController
没有从任何地方连接
很难给你一个答案
您可以在SomeController1
和SomeController
两者之间创建委托
希望您清楚Bluetooth Operations的目标。getWatchCollectedData=Self也尝试过。但是没有起作用@您的
BluetoothOperations
对象是全局对象吗,或者显示如何创建instancelet object=BluetoothOperations(),object.getWatchCollectedData=self@JonSnow是的,它是全球性的。更新ques.ok,我照你说的做了,这个函数是从viewDidLoad调用的。@sharadchauhan。可以这意味着您的代理系统工作正常。事情出了差错,正是我所说的。委托未在您正在运行的实际BluetoothOperations
对象上设置。因此,请确保从控制器和委托初始化的BluetoothOperations
对象设置正确。@sharadchauhan是的,这是问题所在,您的customFunc
不是从您从SomeController
@Jonsow创建的实例调用的。sharadchauhan:好吧,这取决于你的代码运行的BluetoothOperations
实例存在的方式、地点和时间。您可以尝试从BluetoothOperations
类创建单例对象,并确保没有重复的实例。