从所有者的拥有对象调用方法
我有一个名为 RootViewController
的表视图类和一个名为 WifiClass
的提供 WiFi 功能的类。当我加载 RootViewController
类时,我在 WifiClass
上调用名为 setup
的方法,该方法将执行 Wifi 连接初始化。
当应用程序运行时,如果任何连接的设备向我的设备发送一些数据,Wifi 类中有一个流处理程序委托将触发。当时,我需要从我的 RootViewController
调用名为 myMethod
的方法。请问谁能告诉我一个正确执行此操作的好方法?
I have a table view class called RootViewController
and a class providing WiFi functionality called WifiClass
. When I load the RootViewController
class, I am calling a method named setup
on WifiClass
, which will do the Wifi connection initialization.
While the app is running, if any connected device sends some data to my device, there is a stream handler delegate in the Wifi class which will trigger. At that time, I need to call a method named myMethod
from my RootViewController
. Please can anyone tell me a good way to do this properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Objective-C 中有不同的概念,例如
There a different conceptes in Objective-C like
我假设您的意思是流处理程序是 WifiClass 的委托?在这种情况下,请将 RootViewController 设置为 WifiClass 的委托。在 RootViewController 中实现的委托回调中,调用 RVC 中的 myMethod:
对注释的响应:
在您的 WifiClass 中,您必须为委托创建一个实例变量。
然后在您的 RootViewController 中,您必须实现委托并连接起来:
希望这会有所帮助!
I'm assuming you mean the stream handler is a delegate of the WifiClass? In that case, set your RootViewController as the delegate of the WifiClass. In the delegate callback, implemented in RootViewController, call myMethod in RVC:
Response to comments:
In your WifiClass, you'll have to create an instance variable for the delegate.
Then in your RootViewController, you have to implement the delegate and hook things up:
Hope this helps!
试试这个
Try this
您可以做的事情很少。一种是对要调用其方法的对象有一个弱引用。在这种情况下,视图控制器就是对象。在 wifi 类中声明
MainViewController
的可分配属性(假设这是该类),并在初始化期间将其设置为视图控制器。由于您拥有对视图控制器的引用,因此您可以在委托方法中调用所需的方法。另一种方法是使用
块
。该块的定义可以是 -您现在可以在初始化期间传递更新块,
其中可能有很多内容。如果还不清楚,请告诉我。阅读 GCD。在我看来,这是一个非常强大的工具。
There are few things you can do. One is to have a weak reference to the object whose method you want to invoke. In this case the view controller is the object. Declare an
assign
-able property of theMainViewController
(assuming this is the class) in the wifi class and set it to the view controller during initialization. Since you've a reference to the view controller, you can invoke the method you want in the delegate method.Another approach is to use
Blocks
. The block's definition can be -You can now pass the update block during initialization,
There is probably a lot in there. Let me know if it is still unclear. Read up on GCD. It is a very powerful tool in my opinion.
只需发送通知......
像这样发布......
像这样接收它......
你的方法......
....
....
....
你的代码
它
删除
just send an notification......
post it like this...
receive it like this...
your method....
....
....
....
ur code
}
remove it
为什么不将
welcomemessage
方法移至appDelegate
中。这更有意义,因为我假设“消息”不需要与任何特定的视图控制器关联。因此,当您的
wifi delegate
被触发时,只需引用appDelegate
并调用该方法:Why not move the
welcomemessage
method to theappDelegate
. This would make more sense as I assume the "message" does not need to be associated with any particular view Controller.So when your
wifi delegate
is triggered just reference theappDelegate
and call the method: