[英]Unable to call another class method using delegate in swift
I'm trying to call method of Class B from class A on the button tap event. 我正在尝试在按钮轻击事件中从A类调用B类的方法。 But it does not work and below is my code.
但这不起作用,下面是我的代码。
// Viewcontroller
class ViewController: UIViewController {
@IBAction func btnClicked(_ sender: Any) {
var objA = A()
objA.delegate?.TestA()
}
}
// ClassA.swift
protocol TestA {
func TestA()
}
class A {
var delegate: TestA?
}
// ClassB.swift
class B : TestA {
func TestA() {
print(" Function A from b")
}
}
When tapping a button, function TestA() does not invoke. 当点击一个按钮时,函数TestA()不会被调用。
I even tried the code below, but it also didn't work: 我什至尝试了下面的代码,但是也没有用:
var objB = B()
var objA = A()
objA.delegate = objB
Because you instantiate instance of Class A using 因为您使用实例化了Class A的实例
var objA = A()
Clearly you haven't initialised delegate
property in A
because its optional its default value is nil 显然,您尚未在
A
初始化delegate
属性,因为它的可选值默认值为nil
Now when you call 现在当你打电话
objA.delegate?.TestA()
delegate is nil hence function TestA will not be called 委托为nil,因此不会调用函数TestA
Suggestion 建议
Always use camelCasing for declaring names of functions. 始终使用camelCasing声明函数名称。 So
TestA()
is incorrect rather use testA()
所以
TestA()
是不正确的,而是使用testA()
EDIT 1: 编辑1:
Tested this 测试了这个
@IBAction func btnClicked(_ sender: Any) {
let objA = A()
let objB = B()
objA.delegate = objB
objA.delegate?.TestA()
}
This is working fine what is the issue? 这工作正常,这是什么问题?
The objA.delegate is never assigned to an object, so it has an initial value of nil. objA.delegate从未分配给对象,因此其初始值为nil。 The ?
? operator avoids calling a function on a nil object.
运算符避免在nil对象上调用函数。
The answer by Sandeep Bhandari is right. Sandeep Bhandari的答案是正确的。
Some information for better understanding of Protocol and Delegates. 一些信息,可以更好地理解协议和代表。
TestA
is a protocol
and an optional var delegate
is defined in class A
. TestA
是一个protocol
并且在class A
定义了一个可选的var delegate
。 This setup is right. 此设置是正确的。 The idea behind this setup is that any user of
class A
, in this case class B
which conforms to protocol TestA
gets a callback from class A
. 此设置背后的想法是,
class A
任何用户(在本例中为符合protocol TestA
class B
都会从class A
获得回调。 You need to call the delegate.testA()
function from within class A
. 您需要从
class A
内调用delegate.testA()
函数。 The current implementation of ViewController
is not at all benefiting from defining Protocol and Delegates. ViewController
的当前实现根本无法从定义协议和委托中受益。
To achieve proper usage, the class A
cab be modified as follows: 为了实现正确的使用,可以对
class A
出租车进行如下修改:
protocol TestA {
func testA()
}
class A {
var delegate: TestA?
func process() {
// Do something and call delegate function to report it.
delegate?.testA()
}
}
And modify ViewController
as follows (copied class B
for completeness): 并按如下方式修改
ViewController
(为完整ViewController
,请复制class B
):
class ViewController: UIViewController {
@IBAction func btnClicked(_ sender: Any) {
var objA = A()
var objB = B()
objA.delegate = objB
objA.process()
}
}
// ClassB.swift
class B : TestA {
func TestA() {
print(" Function A from b")
}
}
Now function implemented in class B
to conform to protocol TestA
will be called when process()
function on objA
of class A
is called. 现在函数中实现
class B
以符合protocol TestA
时会被调用process()
对函数objA
的class A
被调用。 This is better use of Protocol and Delegate. 这是协议和代理的更好使用。 Hope this helps.
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.