Calling a method from an owned object on the owner
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开发者_C百科 do this properly?
There a different conceptes in Objective-C like
- Delegates
- Notifications
- Key-Value-Observing
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!
Try this
[[UIApplication sharedApplication] sendAction:@selector(yourMethod) to:nil from:self forEvent:someEvent];
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.
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];
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];
精彩评论