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
Related
I have two classes, one view controller an another NSObject class that performs async processes, once its done I want to interrupt the view controller similar to what a button does as IBAction, to update UI. I wanted to keep this functionality in modularized for better use in later stages. Is there a way to do this on Objective-c?
CircleDetectionViewController.h:
#import <UIKit/UIKit.h>
#import "CameraController.h"
#interface CircleDetectionViewController : UIViewController <MyDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *screenPreview;
#end
CircleViewController.mm:
#interface CircleDetectionViewController () <CvVideoCameraDelegate>
#end
#implementation CircleDetectionViewController
CvVideoCamera *camera;
//code
#end
CameraController.h:
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import "CircleDetectionViewController.h"
#protocol MyDelegate <NSObject>
-(void) updateUI;
#end
#interface CameraController : NSObject <MyDelegate>
#property (nonatomic, strong) AVCaptureDevice *inputDevice;
#property (nonatomic, strong) AVCaptureSession *session;
#property (nonatomic, strong) AVCaptureStillImageOutput *photoOutput;
#property (nonatomic, weak) id <MyDelegate> delegate;
#end
CameraController.m:
#import "CameraController.h"
#import "FlashHelper.h"
#import "ImageProcessor.h"
#implementation CameraController
#synthesize delegate;
FlashHelper *Flash;
ImageProcessor *IMGProcessor;
int shootCounter;
int NUMSHOTS = 2;
-(id)init{
//code
}
For some reason the code says "No type or protocol named MyDelegate"
Any help would be really appreciated!
In NSObject class create a delegate:
#protocol MyDelegate <NSObject>
-(void) updateUI;
#end
and declare a property of delegate
#property (nonatomic, weak) id <MyDelegate> delegate;
In viewcontroller class invoke the delegate class
NSObject class obj.delegate = self;
and implement the delegate method.
Always update your UI from Main Thread.
To get the main thread use this:
dispatch_async(dispatch_get_main_queue(), ^{
// call your delegate method or update UI here
});
For details for delegate methods check this.
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
//---------
}
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.
I have a subclassed UIView called TargetView that is contained in a UIViewController called MainViewController. I want to set MainViewController as the delegate for TargetView so that MainViewController can receive messages from the child view (TargetView).
In my MainViewController (UIViewController) header I have the following:
#import <UIKit/UIKit.h>
#import "TargetView.h"
#class TargetView;
#interface MainViewController : UIViewController <TargetViewDelegate>
#property (strong, nonatomic) IBOutlet TargetView *target;
#property (strong, nonatomic) IBOutlet UILabel *lblResults;
#end
When I set the TargetViewDelegate in the interface declaration, it shows up in code completion so it knows that it's there, but then the build fails with the message: can't find protocol declaration..."
In my TargetView (UIView) class I have the following:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "MainViewController.h"
#protocol TargetViewDelegate
#required
-(void)receivedTargetTap;
#end
#interface TargetView : UIView{
id<TargetViewDelegate> delegate;
}
#property (nonatomic,strong) NSString *lblResults;
#property (nonatomic,weak) id<TargetViewDelegate> delegate;
#end
Creating custom delegates is uncharted territory for me. Can anyone tell me what I'm doing wrong? Thanks!
I believe your TargetView.h should be :
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#protocol TargetViewDelegate <NSObject>
#required
-(void)receivedTargetTap;
#end
#interface TargetView : UIView
#property (nonatomic, strong) NSString *lblResults;
#property (nonatomic, weak) id<TargetViewDelegate> delegate;
#end
MainViewController.h :
#import <UIKit/UIKit.h>
#import "TargetView.h"
#interface MainViewController : UIViewController <TargetViewDelegate>
#property (strong, nonatomic) IBOutlet TargetView *target;
#property (strong, nonatomic) IBOutlet UILabel *lblResults;
#end
From your code, you must add <NSObject> after your protocol definition, and remove the MainViewController.h import in your TargetView class.
I think there might be a problem with your import statements in both .h files.
Why do you reference MainViewController.h from TargetView.h? It seems like you don't need it. On the other hand, you should remove the forward declaration of #class TargetView in MainViewController.h, and the simple #import "TargetView.h" should be enough.
After that, you'll also need to implement the required - (void)receivedTargetTap;, otherwise the compiler will complain again that the TargetViewDelegate is not fully implemented.
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