从所有者的拥有对象调用方法

发布于 2024-11-09 05:38:12 字数 361 浏览 0 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

污味仙女 2024-11-16 05:38:12

Objective-C 中有不同的概念,例如

There a different conceptes in Objective-C like

飘落散花 2024-11-16 05:38:12

我假设您的意思是流处理程序是 WifiClass 的委托?在这种情况下,请将 RootViewController 设置为 WifiClass 的委托。在 RootViewController 中实现的委托回调中,调用 RVC 中的 myMethod:

// RootViewController.m
- (void)delegateCallback {
    [self myMethod];
}

对注释的响应:
在您的 WifiClass 中,您必须为委托创建一个实例变量。

@protocol WifiStreamDelegate
- (void)handleNewStream:(id)someStreamObject;
@end

@interface WifiClass : NSObject {
    // *delegate* is an object that conforms to the WifiStreamDelegate protocol
    id<WifiStreamDelegate> delegate;
    // …Other instance variables    
}
// You don't want to own your delegate
// Use the *assign* flag
@property (nonatomic, assign) id<WifiStreamDelegate> delegate;
// …Other properties

@end

@implementation WifiClass
@synthesize delegate;
// …Other methods
@end

然后在您的 RootViewController 中,您必须实现委托并连接起来:

#import "WifiClass.h"
@interface RootViewController : UITableViewController<WifiStreamDelegate>
{
    WifiClass *wifi;
    // …Other instance variables
}
// *wifi* is now an object you own—retain it
@property (nonatomic, retain) WifiClass *wifi
// …Other properties
@end

@implementation RootViewController
@synthesize wifi;

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (!(self = [super initWithCoder:aDecoder]))
        return nil;
    if (!self.wifi)
        wifi = [[WifiClass alloc] init];
    // Set delegate
    wifi.delegate = self;
}

- (void)myMethod {
    // Do something
}

// Delegate method
- (void)handleNewStream:(id)someStreamObject {
    // Handle stream
    [self myMethod];
}
@end

希望这会有所帮助!

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:

// RootViewController.m
- (void)delegateCallback {
    [self myMethod];
}

Response to comments:
In your WifiClass, you'll have to create an instance variable for the delegate.

@protocol WifiStreamDelegate
- (void)handleNewStream:(id)someStreamObject;
@end

@interface WifiClass : NSObject {
    // *delegate* is an object that conforms to the WifiStreamDelegate protocol
    id<WifiStreamDelegate> delegate;
    // …Other instance variables    
}
// You don't want to own your delegate
// Use the *assign* flag
@property (nonatomic, assign) id<WifiStreamDelegate> delegate;
// …Other properties

@end

@implementation WifiClass
@synthesize delegate;
// …Other methods
@end

Then in your RootViewController, you have to implement the delegate and hook things up:

#import "WifiClass.h"
@interface RootViewController : UITableViewController<WifiStreamDelegate>
{
    WifiClass *wifi;
    // …Other instance variables
}
// *wifi* is now an object you own—retain it
@property (nonatomic, retain) WifiClass *wifi
// …Other properties
@end

@implementation RootViewController
@synthesize wifi;

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (!(self = [super initWithCoder:aDecoder]))
        return nil;
    if (!self.wifi)
        wifi = [[WifiClass alloc] init];
    // Set delegate
    wifi.delegate = self;
}

- (void)myMethod {
    // Do something
}

// Delegate method
- (void)handleNewStream:(id)someStreamObject {
    // Handle stream
    [self myMethod];
}
@end

Hope this helps!

热鲨 2024-11-16 05:38:12

试试这个

[[UIApplication sharedApplication] sendAction:@selector(yourMethod) to:nil from:self forEvent:someEvent];

Try this

[[UIApplication sharedApplication] sendAction:@selector(yourMethod) to:nil from:self forEvent:someEvent];
听你说爱我 2024-11-16 05:38:12

您可以做的事情很少。一种是对要调用其方法的对象有一个弱引用。在这种情况下,视图控制器就是对象。在 wifi 类中声明 MainViewController 的可分配属性(假设这是该类),并在初始化期间将其设置为视图控制器。由于您拥有对视图控制器的引用,因此您可以在委托方法中调用所需的方法。

另一种方法是使用。该块的定义可以是 -

typedef void (^UpdateHandler)(void);

...

@interface WiFiConnection:NSObject <...> {
    ...
    UpdateHandler updateHandler;
}
...
- (void)setUpdateHandler:(UpdateHandler)handler;
@end

@implemention WiFiConnection
...
- (void)setUpdateHandler:(UpdateHandler)handler {
    updateHandler = handler;
}
...
- (void)delegateMethodFromWhichYouWantToInvoke {
    ...
    if ( updateHandler != NULL ) {
        dispatch_async(dispatch_get_main_queue(), updateHandler);
    }
}
...
@end

您现在可以在初始化期间传递更新块,

WiFiConnection *connection = [[WiFiConnection alloc] init];
...
__block MainViewController *controller = self;
[connection setUpdateHandler:^{
    [controller welcomeMessage];
}];

其中可能有很多内容。如果还不清楚,请告诉我。阅读 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 the MainViewController (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 -

typedef void (^UpdateHandler)(void);

...

@interface WiFiConnection:NSObject <...> {
    ...
    UpdateHandler updateHandler;
}
...
- (void)setUpdateHandler:(UpdateHandler)handler;
@end

@implemention WiFiConnection
...
- (void)setUpdateHandler:(UpdateHandler)handler {
    updateHandler = handler;
}
...
- (void)delegateMethodFromWhichYouWantToInvoke {
    ...
    if ( updateHandler != NULL ) {
        dispatch_async(dispatch_get_main_queue(), updateHandler);
    }
}
...
@end

You can now pass the update block during initialization,

WiFiConnection *connection = [[WiFiConnection alloc] init];
...
__block MainViewController *controller = self;
[connection setUpdateHandler:^{
    [controller welcomeMessage];
}];

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.

擦肩而过的背影 2024-11-16 05:38:12

只需发送通知......

像这样发布......

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyMethodNotification" object:self];

像这样接收它......

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MyMethod:) name:@"MyMethodNotification" object:nil];

你的方法......

  • (void)MyMethod:(NSNotification *)notification {
    ....
    ....
    ....
    你的代码

删除

[[NSNotificationCenter defaultCenter] removeObserver:self];

just send an notification......

post it like this...

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyMethodNotification" object:self];

receive it like this...

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MyMethod:) name:@"MyMethodNotification" object:nil];

your method....

  • (void)MyMethod:(NSNotification *)notification {
    ....
    ....
    ....
    ur code
    }

remove it

[[NSNotificationCenter defaultCenter] removeObserver:self];
白鸥掠海 2024-11-16 05:38:12

为什么不将 welcomemessage 方法移至 appDelegate 中。这更有意义,因为我假设“消息”不需要与任何特定的视图控制器关联。

因此,当您的 wifi delegate 被触发时,只需引用 appDelegate 并调用该方法:

[[[UIApplication sharedApplication] delegate] welcomeMessage];

Why not move the welcomemessage method to the appDelegate. 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 the appDelegate and call the method:

[[[UIApplication sharedApplication] delegate] welcomeMessage];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文