When I tried having protocols in below two classes, compiler says that the protocol declarations cannot be found
ViewController:
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
#protocol FlipOtherSideViewControllerDelegate;
#interface ViewController : UIViewController<FlipsideViewControllerDelegate> {
id <FlipOtherSideViewControllerDelegate> __unsafe_unretained delegate;
}
- (IBAction)switchMode:(id)sender;
#property (unsafe_unretained) id <FlipOtherSideViewControllerDelegate> delegate;
#end
#protocol FlipOtherSideViewControllerDelegate
- (void)flipothersideViewControllerDidFinish:(ViewController *)controller;
#end
SecondViewController:
#import <UIKit/UIKit.h>
#import "ViewController.h"
#protocol FlipsideViewControllerDelegate;
#interface SecondViewController : UIViewController <FlipOtherSideViewControllerDelegate> {
id <FlipsideViewControllerDelegate> __unsafe_unretained delegate;
}
#property (unsafe_unretained) id <FlipsideViewControllerDelegate> delegate;
#end
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(SecondViewController *)controller;
#end
Any suggestion on above?
Thanks in advance.
Why you declaring below the interface? Try to declare just before the interface declaration. I think there should not be any error then.
Related
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];
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>
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.
Since I switched my iOS project to ARC, I keep getting this compiler error:
No visible #interface for 'CDViewController' declares the selector
'setUseAgof:'
In this line:
[self.viewController setUseAgof:false];
In this file:
AppDelegate.m
#import "AppDelegate.h"
#import "MainViewController.h"
#import <Cordova/CDVPlugin.h>
#import "NSString+MD5.h"
#implementation AppDelegate
#synthesize window, viewController;
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
// (...)
[self.viewController setUseAgof:false]; // <-- HERE
// (...)
return YES;
}
#end
Although the method is definitely defined:
MainViewController.h
#import <Cordova/CDVViewController.h>
#import <ADTECHMobileSDK/ADTECHMobileSDK.h>
#interface MainViewController : CDVViewController <ATInterstitialViewDelegate>{
ATInterstitialView *interstitial;
}
- (void)setUseAgof:(BOOL)useAgofParam;
#end
#interface MainCommandDelegate : CDVCommandDelegateImpl
#end
#interface MainCommandQueue : CDVCommandQueue
#end
MainViewController.m
#import "MainViewController.h"
#interface MainViewController()
- (void)setUseAgof:(BOOL)useAgofParam;
#end
#implementation MainViewController
BOOL useAgof = true;
- (void)setUseAgof:(BOOL)useAgofParam
{
NSLog(#"1.) Setting useAgof = %d", (int)useAgofParam);
useAgof = useAgofParam;
}
I don't get it. What's wrong?
Update:
AppDelegate.h
#import <UIKit/UIKit.h>
#import <Cordova/CDVViewController.h>
#import <INFOnlineLibrary/INFOnlineLibrary.h>
#interface AppDelegate : NSObject <UIApplicationDelegate>{}
#property (nonatomic, strong) IBOutlet UIWindow* window;
#property (nonatomic, strong) IBOutlet CDVViewController* viewController;
#end
viewController seems to be declared as pointer of CDVViewController. You are calling a method which is part of MainViewController the class derived from CDVViewController. Method being called is part of derived class and not the base class, hence make viewController pointer of MainViewController instead.
Make sure the viewController property in your AppDelegate has a type of CDVViewController * rather than UIViewController *.
I was playing around with MKMap and some custom delegate it's not working here and i really don't know why :/
Here's my code :
LocationViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#protocol LocationViewDelegate <NSObject>
#required
- (void)didReceiveLocation:(CLLocation *)location;
#end
#interface LocationViewController : UIViewController <CLLocationManagerDelegate>
#property (strong, nonatomic) IBOutlet UILabel *locationLabel;
#property (nonatomic, strong) id<LocationViewDelegate> delegate;
- (IBAction)sendLocation:(id)sender;
#end
LocationViewController.m
[...]
- (IBAction)sendLocation:(id)sender {
if ([_delegate respondsToSelector:#selector(didReceiveLocation:)]) {
[_delegate didReceiveLocation:_currentLocation];
} else {
NSLog(#"nope");
}
}
[...]
MapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "LocationViewController.h"
#interface MapViewController : UIViewController <LocationViewDelegate>
#end
MapViewController.m
[...]
- (void)didReceiveLocation:(CLLocation *)location {
_mapView.centerCoordinate = location.coordinate;
}
[...]
I'm always getting the "nope" message means that respondsToSelector returns NO.
I did pretty the same example a few days ago and everything was fine.
Someone can see where's the problem here ?
Set your MapViewController as the delegate for the LocationViewController i.e. in your MapViewController.m:
self.locationViewController.delegate = self;
Just as an aside, I'm presuming your MapViewController owns a LocationViewController instance, if so it's better to set the delegate property in LocationViewController to 'weak' to avoid a circular reference. #property (nonatomic, weak) id delegate;