Delegate for the iOS - 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

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
//---------
}

Trying to call custom delegate method of subclass of UITextField in iOS

I have a custom delegate that I have created for a subclass of UITextField. In the delegate class, I've declared an enum like this:
MyCustomDelegate.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "MyCustomTextField.h"
#interface MyCustomDelegate : NSObject <MyDelegate, UITextFieldDelegate>
#property (nonatomic, strong)MyCustomTextField *customTextField;
#end
MyCustomTextField.h:
#import <UIKit/UIKit.h>
typedef enum {
EnumTypeA,
EnumTypeB,
EnumTypeC,
EnumTypeD,
EnumTypeE
} MyEnumType;
#class MyCustomDelegate;
#protocol MyDelegate <NSObject>
#required
- (void)methodA;
- (void)methodB;
#end
#interface MyCustomTextField : UITextField
#property (nonatomic, weak)id <MyDelegate>myDelegate;
#property (nonatomic) MyEnumType enumType;
#end
Now, I am trying to use this enum in conjunction with my custom UITextField elsewhere in my project, like this:
MyViewController.h
#import "MyCustomTextField.h"
#import "MyCustomDelegate.h"
#import <UIKit/UIKit.h>
#interface MyViewController : UIViewController
#property (weak, nonatomic) IBOutlet MyCustomTextField *mySampleTextField;
#end
MyViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
[self.mySampleTextField setMyEnumType:EnumTypeA];
}
However, I am getting the error, "No visible #interface for 'MyCustomTextField' declares the selector 'setMyEnumType'".
Can anyone see what it is I'm doing wrong?
Objective-C will automatically generate setter for you based on the name of the property (enumType) instead of the name of type (MyEnumType). So your setters should be the following instead:
[self.mySampleTextField setEnumType:EnumTypeA];

Cannot find protocol declaration ios7

I've declared a protocol named ScrollableTimelineViewProtocol in a file ScrollableTimelineView.h as below :
#import <UIKit/UIKit.h>
#import "AbstractView.h"
#import "RedTimeIndicator.h"
#import "EventsModel.h"
#import "TimeStrands.h"
#define LABEL_TAG_OFFSET -500
#protocol ScrollableTimelineViewProtocol <NSObject>
- (void) showPopup : (NSInteger)tag;
#end
#interface ScrollableTimelineView : UIView<TimeStrandDelegate,UIScrollViewDelegate>
{
}
#property (nonatomic,assign) id<ScrollableTimelineViewProtocol> delegate;
And I'm trying to use it in a view controller :
#import <UIKit/UIKit.h>
#import "AbstractViewController.h"
#import "TimeStrands.h"
#import "ScrollableTimelineView.h"
#protocol TimelineDelegate <NSObject>
- (void) detailedShownDelegate;
- (void) detailedViewHiddenDelegate;
#end
#interface TimelineViewController : UIViewController<ScrollableTimelineViewProtocol>;
But I get an error saying Could not find protocol declartion for ScrollableTimelineViewProtocol. Help.
There should not be semicolon at the end
Import the class where ScrollableTimelineViewProtocol declared.
#protocol scrollableTimelineViewProtocol;
#property (strong, nonatomic) IBOutlet UITableView *tblView;
#property (strong, nonatomic) IBOutlet id<scrollableTimelineViewProtocol> delegate;
#end
#protocol scrollableTimelineViewProtocol <NSObject>
-(void)runFast;
#end
Just simple... Try this...
Remove semocolon from interface..
#interface TimelineViewController : UIViewController<ScrollableTimelineViewProtocol>;
Into
#interface TimelineViewController : UIViewController<ScrollableTimelineViewProtocol>

Custom Delegate cannot find declaration

I'm creating a shopping list app, and trying to implement a custom delegate when editing an item. When creating the #protocol at the bottom of the header file, when trying to declare a property of that protocol in the #interface section I'm getting an error of: Cannot find protocol declaration for GEMEditItemViewControllerDelegate
This is what my header file looks like.
#import <UIKit/UIKit.h>
#import "GEMItem.h"
#interface GEMEditItemViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
#property GEMItem *item;
#property (weak) id<GEMEditItemViewControllerDelegate> delegate;
#end
#protocol GEMEditItemViewControllerDelegate <NSObject>
#required
- (void)controller:(GEMEditItemViewController *)controller didUpdateItem:(GEMItem *)item;
#end // End of delegate protocol
Alternatively in a separate instance when declaring the protocol above the interface I cannot access the view controller to pass as a parameter for that declaration method.
That header file looks like:
#import <UIKit/UIKit.h>
#import "GEMItemManager.h"
#protocol GEMAddItemViewControllerDelegate <NSObject>
/*
// Tried to add the controller (controller:(GEMAddItemviewController *)controller) as first paramiter, but was getting and errror, so I have omitted it for the time being
- (void)controller:(GEMAddItemViewController *)controller didSaveItemWithName:(NSString *)name andQuantity:(float)quantity andPrice:(float)price andCategory:(NSString *)category andNotes:(NSString *)notes;
*/
- (void)didSaveItemWithName:(NSString *)name andQuantity:(float)quantity andPrice:(float)price andCategory:(NSString *)category andNotes:(NSString *)notes;
#end
#interface GEMAddItemViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
#property (weak) id<GEMAddItemViewControllerDelegate> delegate;
#property NSArray *categories;
#end
Any thoughts on how to correct this would be greatly appreciated!!
You can do it like this also
#protocol GEMEditItemViewControllerDelegate;
#interface GEMEditItemViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
#property GEMItem *item;
#property (weak) id<GEMEditItemViewControllerDelegate> delegate;
#end
#protocol GEMEditItemViewControllerDelegate <NSObject>
#required
- (void)controller:(GEMEditItemViewController *)controller didUpdateItem:(GEMItem *)item;
#end
Try it this way:
#class GEMEditItemViewController; // forward declaration of class
#protocol GEMEditItemViewControllerDelegate <NSObject>
#required
- (void)controller:(GEMEditItemViewController *)controller didUpdateItem:(GEMItem *)item;
#end // End of delegate protocol
#interface GEMEditItemViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
#property GEMItem *item;
#property (weak) id<GEMEditItemViewControllerDelegate> delegate;
#end
BTW - you should move the two picker view protocols from the header file to a class extension in the .m file. The world doesn't need to know this implementation detail.
Your header file should looks like this:
#class GEMEditItemViewController;
#class GEMItem;
#protocol GEMEditItemViewControllerDelegate <NSObject>
#required
- (void)controller:(GEMEditItemViewController *)controller didUpdateItem:(GEMItem *)item;
#end // End of delegate protocol
#interface GEMEditItemViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
#property GEMItem *item;
#property (weak) id<GEMEditItemViewControllerDelegate> delegate;
#end
You should not use direct import in your header file. #class directive is used to prevent cycle dependencies. In your case GEMItem import should be in your meta file.

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

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.

Resources