iPhone . delegates and properties - iphone

I have a viewController (lets call it vcA) and this viewController has a NSArray property declared and synthesized.
NSArray *myProperty;
...
#property (nonatomic, retain) NSArray *myProperty;
and then synthesized on .m
this vcA is a delegate for another viewController, vcB.
Inside vcB I do:
NSArray *getMyPropertyFromDelegate = (NSArray *)[delegate myProperty];
and I receive an error saying warning: Semantic Issue: Instance method '-myProperty' not found (return type defaults to 'id')
I know I can silent this warning changing the line to
NSArray *getMyPropertyFromDelegate = (NSArray *)[(vca*)delegate myProperty];
and importing vcA.h, but I am trying to make vcB as independent as possible, because the delegate can change.
How do I do that working just with the delegate property?
thanks

I suggest you write a custom protocol.
Make vca view controller conforming to the protocol, and in vcB, declare the delegate property :
#property(retain) id <MyProtocol> delegate;
This means the delegate can be any type, as long as it conforms to MyProtocol.
Here is an example.
// MyProtocol.h
#protocol MyProtocol <NSObject>
#property(retain) NSArray *myProperty;
#end
// vca.h
#interface vca : XXXX <MyProtocol> {
....
}
#property(retain) NSArray *myProperty;
// vca.m
#synthesize myProperty; // or provide a getter

According to your comments to Vince's solution, I would say you need to learn a little about protocols as they are (almost?) always used with the concept of delegate.
You can start with http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html
Nice simple code, rich in comments.
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProtocols.html for the official Apple documentation.

Related

using a delegate in a class method

I want to invoke a delegate in class method.
The example below obviously does not work, since the delegate is an instance variable that is accessed within a class method. (Error: instance variable 'delegate' accessed in class method)
Is there an alertnative?
My header file:
// MyClass.h
#import <Foundation/Foundation.h>
#protocol MyDelegate <NSObject>
-(void)update;
#end
#interface MyClass : NSObject
{
id<MyDelegate> delegate;
}
#property (nonatomic, retain) id delegate;
+(void)methodThatInvokesDelegate;
#end
My implementation file:
// MyClass.m
#import "MyClass.h"
#implementation MyClass
#synthesize delegate;
+(void)methodThatInvokesDelegate{
[delegate update];
}
#end
Three obvious options:
Singleton
Static variable (i.e., class variable) pointing to the delegate
Use NSNotification's rather than delegates
Since a singleton (and a static variable) can't keep track of the lifecycle of delegates, I think option three would be the cleanest.
I want to know the context, which let you run in that situation. ;-) Anyway:
First: Delegates are set for a specific instance object. Because of this, you can have different delegates for different instances of the same (delegating) class.
Second: A class method runs inside a class object of that class. This is an object that is different from every instance object of that class. So there is nothing that can be called "the delegate". You can have 100s of delegates.
Third: Your class object needs a delegate at its own. So you have to add a property delegate to the class object and then use this. (Yes, it is possible to have properties an a class object. I did not write declared property.) If you need further information on how to do this, just comment it. I will add code.
I'm not sure if this will help you, but I have a similar situation where I have a class method used for data loads. In this case, the class instantiates itself (so that the caller doesn't need to) until it is done. (this code was edited somewhat to make it work here)
header file:
#protocol DataLoaderDelegate2 <NSObject>
- (void) dataLoaderSuccess:(NSData *)data loader:(id)theloader;
- (void) dataLoaderFailed:(NSString *)error loader:(id)theloader;
#end
#interface DataLoader2 : NSObject {
NSURLConnection *conn;
NSMutableData *receivedData;
NSFileHandle *fileHandle;
id <DataLoaderDelegate2> delegate;
}
#property (nonatomic, assign) id<DataLoaderDelegate2>delegate;
Call to start the process - the call to initWithRequest passes "self" along.
+ (DataLoader2 *)loadWithURLRequest:(NSURLRequest *)req delegate:(id)_delegate
{
DataLoader2 *dl = [[DataLoader2 alloc] init];
[dl setDelegate:_delegate];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
return dl;
}
When the data is done loading, it cleans up with something like
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if ([delegate respondsToSelector:#selector(dataLoaderSuccess:loader:)])
[delegate dataLoaderSuccess:(fileHandle)?(id)fileHandle:(id)receivedData loader:self];
[self autorelease];
}

iOS - Set Model class to be delegate of another Model class

I have a CLLocationManager singleton which implements a protocol, so I can tell another model class (ServerConnection) that an updated location of the user has been found.
In my AppDelegate in the method, didFinishLaunching, I write
ServerConnection* serverConnection = [[ServerConnection alloc] init];
[LocationManager sharedLocationSingleton].delegate = serverConnection;
[[LocationManager sharedLocationSingleton] getUsersLocation];
This doesn't work and the delegate method in my ServerConnection class isn't called. However, if I try having my AppDelegate class be the listener, as in the following line, it works fine.
// self refers to AppDelegate
[LocationManager sharedLocationSingleton].delegate = self;
Here, my AppDelegate implements the required delegate method and the method is called when the user's location is updated, as it should.
Why is my above method failing, where I try to set the delegate to be serverConnection?
Tutorials online usually point to using a UIViewController or the AppDelegate as the "listener", but in my case, I want a separate model class to be the listener. How do I do that?
Below is my LocationManager singleton class with the protocol
#class LocationManager;
#protocol LocationManagerDelegate <NSObject>
#required
-(void)LocationManagerUpdated:(LocationManager*) locationManager
withValue:(CLLocation*) location;
#end
#interface LocationManager : NSObject <CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager* locationManager;
#property (strong, nonatomic) CLLocation* location;
#property (weak, nonatomic) id <LocationManagerDelegate> delegate;
+(LocationManager*)sharedLocationSingleton;
-(void) getUsersLocation;
#end
My header file for Server connection is.
#import <Foundation/Foundation.h>
#import "LocationManager.h"
#interface ServerConnection : NSObject <LocationManagerDelegate>
#end
This works when AppDelegate is set to be the listener, but not my model object ServerConnection. How do I fix this?
Thanks!
There should be no problem in doing what you are trying to do (i.e., having a non-controller class instance to act as a delegate).
This works when AppDelegate is set to be the listener, but not my model object ServerConnection.
Does your ServerConnection class implement the LocationManagerDelegate protocol? (I mean implement as opposed to just declare it in its interface).
Check the LocationManager method in charge for calling the delegate method (LocationManagerUpdated:) and add there a NSLog trace to check that the delegate object is correctly set when you try and send it the message.
EDIT:
ServerConnection* serverConnection = [[ServerConnection alloc] init];
[LocationManager sharedLocationSingleton].delegate = serverConnection;
[[LocationManager sharedLocationSingleton] getUsersLocation];
after you comment, it is clear that the issue stems from instantiating serverConnection in a stack variable and not in a property.
Your approach of making the delegate property a strong property is not correct since it leads to retain cycles. What you need to do is defining a strong serverConnection property in the class that executes the code I pasted above (the app delegate?).
If you don't mind my being rash, if you define the delegate as a strong property, what you are doing is fixing a bug by adding a second bug that hides the first one.
It looks like serverConnection is not retained anywhere and because delegate property is specified as weak, it is released and set to nil.
Check getUsersLocation method and see if delegate is nil at the moment you are trying to call LocationManagerUpdated:withValue:

How to use #protocol in AppDelegate iPhone app?

I am working in iPhone app with 5 screens. I want to refresh the values in the screen 4th in UITabBarController. I have added #protocol in AppDelegate but it is not calling. This is the first time am using #protocol could you please help me to solve this issue,
In AppDelegate.h
#protocol ReloadViewControllerDelegate <NSObject>
-(void) refreshViewController:(NSString *)result;
#end
id refreshViewControllerDelegate;
#property (nonatomic, retain) id refreshViewControllerDelegate;
and i have synthesized.
In AppDelegare.m
#synthesize refreshViewControllerDelegate;
if ([refreshViewControllerDelegate conformsToProtocol:#protocol(ReloadViewControllerDelegate)])
{
[refreshViewControllerDelegate performSelectorOnMainThread:#selector(refreshViewController:) withObject:#"YES" waitUntilDone:NO];
}// Control not come inside of if Condition.... From here i want to update the fourthViewController..
But control not go inside of the if condition. Could you please guide me where am doing wrong?
In my 4th ViewController.h
#import "AppDelegate"
#interface fourthViewController : UIViewController <ReloadViewControllerDelegate>
In my 4th ViewController.m
-(void) refreshViewController:(NSString *)result
{
NSLog(#"Result : %#", result);
}
Can anyone please help me to do this? Thanks in advance.
You need to declare your delegate like this:
#property (nonatomic, weak) IBOutlet id<ReloadViewControllerDelegate> delegate;
The id will work, but by using the <>, you can make sure that the delegate you assign is actually implementing the protocol, you might still have to make sure it responds to selector but that is only if some methods are declared as
#optional
make sure you synthesize it and most important make sure you set it, and it is not nil.
You're getting a warning because you are typing your delegate as an id. An id is a generic type, which means the compiler has no idea of what methods or properties might be available. In order to remove your warning, declare your delegate to be an NSObject:
#property (nonatomic, retain) NSObject <ReloadViewControllerDelegate> *refreshViewControllerDelegate;
By declaring as an NSObject, the compiler now knows about all the methods NSObject has, which will then allow you to call:
-performSelectorOnMainThread:withObject:waitUntilDone:
on your delegate without warnings. Good luck!
Try this:
#protocol ReloadViewControllerDelegate <NSObject>
-(void) refreshViewController:(NSString *)result;
#end
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (weak) id <ReloadViewControllerDelegate>refreshViewControllerDelegate;
#end
In AppDelegate.m
#implementation AppDelegate
#synthesize window, refreshViewControllerDelegate;
...
here Tab4ViewController is name of class.
if ([Tab4ViewController conformsToProtocol:#protocol(ReloadViewControllerDelegate)])
{
[refreshViewControllerDelegate performSelectorOnMainThread:#selector(refreshViewController:) withObject:#"YES" waitUntilDone:NO];
}
...
#end
#import "AppDelegate.h"
#interface Tab4ViewController<ReloadViewControllerDelegate>
...
#end
#implementation Tab4ViewController
...
appDelegate.refreshViewControllerDelegate = self;
...
#end
You are calling this code:
if ([refreshViewControllerDelegate conformsToProtocol:#protocol(ReloadViewControllerDelegate)])
But refreshViewControllerDelegate is this:
id refreshViewControllerDelegate;
conformsToProtocol checks to see if the object declares that it conforms to the protocol, which yours does not. If you want to specify conformity to a protocol you need to:
id<ReloadViewControllerDelegate> refreshViewControllerDelegate;
EDIT
OK, on the performSelectorOnMainThread problem... That method is provided in a category for NSThread, and is not declared in the NSObject protocol. So, if you want to call that, then you need to declare your type as NSObject, which conforms to your protocol.
NSObject<ReloadViewControllerDelegate> refreshViewControllerDelegate;
EDIT
OK, it looks like this is not a simple question about using a protocol, but a full tutorial. Since SO isn't the place for such, I'll try to give a brief one...
A protocol is an interface declaration.
#protocol ReloadChatViewControllerDelegate <NSObject>
- (void)refreshViewController:(NSString *)result;
#end
That says there is a new protocol in town, with the name ReloadChatViewControllerDelegate and it also conforms to the NSObject protocol. Any class that adopts the new protocol must provide an implementation of refreshViewController. You can make a protocol method optional, by putting in an #optional section.
#protocol ReloadChatViewControllerDelegate <NSObject>
- (void)refreshViewController:(NSString *)result;
#optional
- (void)optRefresh;
#end
Now, let's leave the adoption of the protocol for later. Say you are writing generic code, and you just want to know if the object you are given conforms to the protocol, and if so, invoke a method on it. Something like...
#interface Bar : NSObject
#property (nonatomic, weak) NSObject<ReloadChatViewControllerDelegate> *refreshViewControllerDelegate;
- (void)blarg;
#end
Now, the Bar class is providing a delegate property, so that it can be give some object that will help it do some work. However, that delegate object must at least be an NSObject, and conform to the ReloadChatViewControllerDelegate protocol.
Now, ObjC (and C) is quite permissive, so you can force an object to be any type you want, but then you deserve the crash you get. Now, when blarg is called, the delegate is notified to do some work.
Since the property type of the delegate already says it conforms to the given protocol, there is no need to check for conformity. We can just call the delegate method. Note that we must see if the object implements any optional protocol methods.
#implementation Bar
#synthesize refreshViewControllerDelegate = _refreshViewControllerDelegate;
- (void)blarg {
// Do something, then invoke the delegate
[self.refreshViewControllerDelegate
performSelectorOnMainThread:#selector(refreshViewController:)
withObject:#"YES"
waitUntilDone:NO];
if ([self.refreshViewControllerDelegate respondsToSelector:#selector(optRefresh)]) {
[self.refreshViewControllerDelegate optRefresh];
}
}
#end
However, if you want to be generic, and accept any object as a delegate (maybe you want to make it optional that the delegate conforms to some given protocol), then you can accept a plain id and then check to see it it conforms. In that case, you could declare your delegate as just an id (or some other type).
#property (nonatomic, weak) id refreshViewControllerDelegate;
Now, in your code, you need to check for conformity.
- (void)blarg {
// Do something, then invoke the delegate
if ([self.refreshViewControllerDelegate
conformsToProtocol:#protocol(ReloadChatViewControllerDelegate)]) {
[self.refreshViewControllerDelegate
performSelectorOnMainThread:#selector(refreshViewController:)
withObject:#"YES" waitUntilDone:NO];
if ([self.refreshViewControllerDelegate
respondsToSelector:#selector(optRefresh)]) {
[self.refreshViewControllerDelegate optRefresh];
}
}
}
OK, now you have a protocol defined, and you have code that calls methods on the protocol. Two caveats.
First, the delegate has to be set to an object. nil will respond false for any method, so it will of course not conform, nor do anything when sent any message.
Second, you have to make sure that your delegate declares conformity to the protocol. Just implementing the methods is not conformity. If a class does not explicitly specify that is conforms to a protocol, then conformsToProtocol will return false, even if it implements the methods of the protocol.
So, let's specify some class that will act as our delegate by conforming to the protocol.
#interface Foo : NSObject<ReloadChatViewControllerDelegate>
- (void)refreshViewController:(NSString *)result;
#end
#implementation Foo
- (void)refreshViewController:(NSString *)result {
NSLog(#"Look, ma, I'm refreshed with %#", result);
}
#end
It conforms to the protocol, provides an implementation for the mandatory method, and omits the optional one.
Now, if you ran this code, you should see that marvelous code in all its splendor.
Foo *foo = [[Foo alloc] init];
Bar *bar = [[Bar alloc] init];
bar.refreshViewControllerDelegate = foo;
[bar blarg];

How to use delegates in ios 5

I need to use a delegate object in an iOS application. I have declared the delegate as this:
In the class where the function is defined:
#interface OOObjectCommandInterface : NSObject<OOCameraControllerDelegate>
In the class where the function must be invoqued:
(In de .h file)
#protocol OOCameraControllerDelegate
- (void)drawFrame:(CVImageBufferRef) imageBuffer:(BOOL)flip;
#end
and
#interface OOCameraController : UIViewController
{
...
id<OOCameraControllerDelegate> delegate;
}
#property (nonatomic, readwrite) id<OOCameraControllerDelegate> delegate;
Aditionally, where the second class is initialized:
_hardwareController.delegate = [OOObjectCommandInterface ocInterface];
where _hardwareController is an instance of OOCameraController class.
So, when I try to invoque the delegate object, I do this:
[delegate drawFrame:imageBuffer:flip];
but the function is not executed. Any idea?
P.D.: The function I am calling is a singleton class. Could be any problem there?
Have you set delegate to self in the second class? Create an object in the second class like
#property (nonatomic, readwrite) id<OOCameraControllerDelegate> delegate;
and then [_hardwareController setDelegate:self];
By definition, a singleton is a design patron to access an object, unique, that only can be created 1 time (first get_instance you do). With get_instance, you can access from everywhere, to the functions inside the singleton, so, Why you are not using it directly?
Write something like [[MySingletonClass get_instance] FunctionThatIWantToUse:...]; And don't use a delegate

ARC issue - cannot pass method to delegate?

Here's my scenario. I have a class A. Inside its implementation I create object of type B and set B's delegate to self (So B.delegate = self somewhere inside class A's implementation).
And class A has an instance method - (void)printThis;
Now inside B's implementation, when I try to do [delegate printThis];, it gives me this error:
"No known instance method for selector printThis"
Of course this is when I have enabled ARC. The above delegation pattern used to work fine in iOS 4.x without the ARC. And it still does when I switch OFF ARC. What has ARC got to do with passing messages to delegates?
Skeleton code:
A.h
#class B;
#interface A: blah blah
{
B objB;
}
-(void) printThis;
A.m
objB = [[B alloc] init];
objB.delegate = self;
- (void)printThis {
//doSomething
}
B.h
#interface B: blah blah
{
//id delegate; //used to be there, now I just property & synthesize
}
#property (nonatomic,weak) id delegate;
B.m
#synthesize delegate;
[delegate printThis]; //error with ARC ON, works with OFF
IMPORTANT EDIT:
And mind you this happens for a method here and there. For instance I have a few other methods in A like printThat etc etc which work without errors. I'm clueless as to what is happening!
You need to define -printThis in a protocol and make A implement this protocol. You also need to mark the delegate as conforming to this delegate.
i.e.:
#protocol Printer <NSObject>
- (void)printThis;
#end
#interface A : NSObject <Printer>
//...
#end
#interface B : //...
#property (nonatomic, weak) id<Printer> delegate;
#end
ARC needs to know about the interface for method calls in order to properly manage the memory correctly. If there isn't a definition then it'll complain.