Objective-c on the protocol defined a property protocol [duplicate] - ios

This question already has answers here:
iOS how to implement a protocol's #property
(3 answers)
Closed 9 years ago.
I read some code, I founded the #protocol have defined a #property's protocol.
For example
protocol1.h
#protocol protocol2;
#protocol protocol1
-(void)p1_method1;
-(void)p1_method2;
#property (readonly, nonatomic) id<protocol2>p2;
#end
protocol2.h
#protocol protocol2
-(void)p2_method1;
-(void)p2_method2;
#end
I don't know the protocol have a #property protocol mean.
Have a simple example? Thanks.

You have to add protocol above the interface you will be using.
#protocol MyViewControllerDelegate;
#interface MyViewController : UIViewController
#property (weak, nonatomic) id <MyViewControllerDelegate> delegate;
#property (copy, nonatomic) NSArray *viewControllers;
#end
#protocol MyViewControllerDelegate <NSObject>
#optional
//sth
#end

All you need to do is #synthesize p2 in a class that conforms to protocol1. Properties from protocols don't get automatically synthesised.
#interface Class1 : NSObject <protocol1>
#end
#implementation Class1
#synthesize p2; // Synthesize p2, the property from protocol1
- (void)p1_method1 {
// Do something
}
- (void)p1_method2 {
// Do something else
}
This will create the correct getter/setter for the property (in your example the property is readonly so only a getter). The #synthesize will also create the ivar, in this case p2.

Related

Cannot find delegate defined in another header

I have a very simple problem, yet because it's so simple, I can't find anything wrong with it to fix.
ListSelectorUtility.h
#import <UIKit/UIKit.h>
#protocol ListSelectorDelegate <NSObject>
- (void) returnValueString:(NSString *)value requestID:(long)requestID;
#end
#interface ListSelectorUtility : UITableViewController
#property (strong, nonatomic) NSString *data;
#property (weak, nonatomic) id<ListSelectorDelegate> delegate;
#property (nonatomic) long requestID;
#end
UpdateProperty.h
#import <UIKit/UIKit.h>
#import "ListSelectorUtility.h"
#interface UpdateProperty : UITableViewController <ListSelectorDelegate>
#property (nonatomic, strong) NSDictionary *data;
#end
There's an error on the #interface UpdateProperty : UITableViewController <ListSelectorDelegate> saying that No type or protocol named 'ListSelectorDelegate'. Why is this happened? How to solve this? Thanks.
EDIT: oh, and when I do CMD+click on the <ListSelectorDelegate>, it brings me straight up to the ListSelectorDelegate declaration. That means the IDE (supposedly) can find it.
set property of delegate (strong) on ListSelectorUtility.h
#property (strong, nonatomic) id<ListSelectorDelegate> delegate;
may bey problem of that because if it is week then dealloc by ARC and call the delegate method on ListSelectorUtility.h on your pass the data and values
[self.delegate returnValueString:obj_StringValue requestID:obj_requestID];
And finally this delegate method declare in UpdateProperty.h,
- (void) returnValueString:(NSString *)value requestID:(long)requestID
{
//---------
//Your Code
//---------
}

Connect subclass's custom delegate for control in Interface Builder

I've got a custom control with a custom delegate:
#class MyButtonSubclass;
#protocol MyButtonSubclass Delegate <NSObject>
#optional
- (void)delegateMethod:(NSString *)param;
#end
#interface MyButtonSubclass : UIButton
#property (nonatomic, weak) id < MyButtonSubclass Delegate> delegate;
#property (nonatomic, strong) NSString* param;
#end
And I've created a button in a Storyboard with the custom subclass:
Is there a way to get the custom delegate to appear in the list of connectable properties in the Storyboard?
Huzzah! The answer was quite simply staring me in the face. The delegate (or dataSource) definitions need to include IBOutlet:
#class MyButtonSubclass;
#protocol MyButtonSubclass Delegate <NSObject>
#optional
- (void)delegateMethod:(NSString *)param;
#end
#interface MyButtonSubclass : UIButton
#property (nonatomic, weak) IBOutlet id < MyButtonSubclass Delegate> delegate;
// ^ This!
#property (nonatomic, strong) NSString* param;
#end

iOS - shortcut to synthesize properties using extension

Let's say i have a class definition header file like this :
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (readonly, strong, nonatomic) SomeObject *managedObject;
#end
instead of defining the #synthesize on managedObject to create the getters/setters a friend of mine told me i can do the following header definition using a class extension to do the synthesis more cleanly:
#import "TSPAppDelegate.h"
#interface TSPAppDelegate () //notice the class extension here
#property (strong, nonatomic) SomeObject *managedObject; //this will already be synthesized since its an extension
#end
Could some one explain how this works using the extensions ?
I think your friend is incorrect. You have to #synthesize to have the getters/setters implemented for you

pass delegating object type as protocol method argument

I want to pass my delegating object as an argument with a declared type so I don't have to cast (if I pass (id)sender instead):
#protocol myObjectDelegate <NSObject>
- (void)myObjectAsArgument:(myObject *)object;
#end
#interface myObject : NSObject
// stuff...
#property (nonatomic, strong) id <myObjectDelegate> delegate;
#end
What would be the correct way of doing this? Like I said, I know I could do this:
- (void)myObjectAsArgument:(id)object;
And that would let me pass self in as argument, but I don't like using the cast syntax.
Thanks.
NOTE:
Apple does that too:
#protocol UITableViewDataSource <NSObject>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
#end
#interface UITableView : UIScrollView
#property (nonatomic, strong) id <UITableViewDataSource> dataSource;
#end
That's just one method from the UITableViewDataSource protocol and it's passing a UITableView type as argument. ;)
Since this is top Google search result, here's how to do it:
#class myObject; // just need this for compiling
#protocol myObjectDelegate <NSObject>
- (void)myObjectAsArgument:(myObject *)object;
#end
#interface myObject : NSObject
// stuff...
#property (nonatomic, strong) id <myObjectDelegate> delegate;
#end
You should do it this way:
- (void)myObjectAsArgument:(id<myObjectDelegate>)object;
with myObjectDelegate being the name of your protocol ;)

Delegate for the iOS

I'm trying to see why the type can't be read from the protocol. I figured that the it's because the #interface is below the protocol but if someone can help me figure out the problem and explain to me why that would be great.
#import <UIKit/UIKit.h>
#protocol ARCHCounterWidgetDelegate <NSObject>
- (UIColor *) counterWidget: (ARCHCounterWidget *) counterWidget;
#end
#interface ARCHCounterWidget : UIView
#property (weak, nonatomic) id<ARCHCounterWidgetDelegate> delegate;
#end
You have to either forward declare the class or the protocol:
// tell the compiler, that this class exists and is declared later:
#class ARCHCounterWidget;
// use it in this protocol
#protocol ARCHCounterWidgetDelegate <NSObject>
- (UIColor *) counterWidget: (ARCHCounterWidget *) counterWidget;
#end
// finally declare it
#interface ARCHCounterWidget : UIView
#property (weak, nonatomic) id<ARCHCounterWidgetDelegate> delegate;
#end
or:
// tell the compiler, that this protocol exists and is declared later
#protocol ARCHCounterWidgetDelegate;
// now you can use it in your class interface
#interface ARCHCounterWidget : UIView
#property (weak, nonatomic) id<ARCHCounterWidgetDelegate> delegate;
#end
// and declare it here
#protocol ARCHCounterWidgetDelegate <NSObject>
- (UIColor *) counterWidget: (ARCHCounterWidget *) counterWidget;
#end
The compiler is not yet aware of the ARCHCounterWidget, and therefore cannot resolve the type in the delegate method. Simple solution is:
#import <UIKit/UIKit.h>
// Reference the protocol
#protocol ARCHCounterWidgetDelegate
// Declare your class
#interface ARCHCounterWidget : UIView
#property (weak, nonatomic) id<ARCHCounterWidgetDelegate> delegate;
#end
// Declare the protocol
#protocol ARCHCounterWidgetDelegate <NSObject>
- (UIColor *) counterWidget: (ARCHCounterWidget *) counterWidget;
#end
It just makes it so that the compiler knows that the protocol is defined SOMEWHERE and can be referenced

Resources