I have searched for related questions, but nothing has worked for me so I will describe my problem. I have two classes. Lets call them ClassA and RootViewController. RootViewController has a button that will trigger an action and delegate it to ClassA. The header for RootController looks like this:
#import <UIKit/UIKit.h>
#protocol RootViewControllerDelegate;
#interface RootViewController : UIViewController <UIPageViewControllerDataSource> {
}
...
#end
#protocol RootViewControllerDelegate <NSObject>
-(void)buttonPressed : (UIButton *) button;
#end
The ClassA header looks like this:
#import "RootViewController.h"
#interface RightPanelViewController : UIViewController <RootViewController>
...
#end
And I get the error :"Cannot find the protocol declaration for 'RootViewController'. Like I said, I have read some questions related to the same topic, the documentation for delegates, but I'm not able to see what the problem is. I would appreciate some help on this matter.
Change your interface line to
#interface RightPanelViewController : UIViewController <RootViewControllerDelegate>
ie. the name in angle brackets must match exactly with the name in your #protocol definition.
Related
i have two view controllers, AbcViewController and XyzViewController. Both controllers behave similarly. Each has a "add" button which opens up a AddNewAbcViewController and AddNewXyzViewController respectively.
On AddNewAbcViewController, when the button "submit" is taped, it does it necessary stuff and close, bringing it back to AbcViewController. I am using delegate here where AbcViewController does the closing of AddNewAbcViewController. This works.
Now I want to do the same for XyzViewController and AddNewXyzViewController, but it is not working. When the btnSubmit is called in AddNewXyzViewController, it didn't enter into XyzViewController dimiss method. I have scanned through my codes many times but don't find anything extra not added. I even gave a different dismiss method name in XyzViewController and AddNewXyzViewController but that didn't work either. What did I miss?
here are my snippets for AbcViewController and AddAbcViewController. The codes for Xyz are identical:
class AddNewAbcViewController.h is
#import <Foundation/Foundation.h>
// protocol
#protocol AddNewAbcProtocol <NSObject>
-(void)dismiss;
#end
#interface AddNewAbcViewController : UIViewController<UITextViewDelegate>
#property(nonatomic, weak)id<AddNewAbcProtocol> delegate;
#end
class AddNewAbcViewController.m is
#interface AddNewAbcViewController() <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
...
#end
#implementation AddNewAbcViewController
...
- (IBAction)btnSubmit:(id)sender
{
[self.delegate dismiss];
}
#end
class AbcViewController.h is
#import <Foundation/Foundation.h>
#import "AddNewAbcViewController.h"
#interface AbcViewController : UIViewController<AddNewAbcProtocol, UISplitViewControllerDelegate>
...
#end
class AbcViewController.m is
#implementation AbcViewController
-(void)dismiss
{
NSLog(#"delegated to dismiss()");
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
As everyone explained, basically you forgot a line of code like ".delegate = self".
Here's a handy beginner's intro to delegates.
https://stackoverflow.com/a/4213005/294884
Use if statement to see if delegate works:
if ([self.delegate respondsToSelector:#selector(dismiss)])
{
[self.delegate dismiss];
}
Create AddNewXyzViewController as an instance variable, but not a local variable.
I just got started working with delegates.
from some reason I cannot do it, and I see errors.
I tried to do the next code on a new project and it was OK, but when i'm moving this code to my main project I have a few issues.
my NSObject class .h:
#protocol myNSObjectClassDelegate <NSObject>
#required
-(void)doSomething;
#end
#interface GeneralMethods : NSObject
#property (nonatomic,strong) id<myNSObjectClassDelegate> delegate;
#end
my NSObject class .m:
#synthesize delegate;
-(void)SomeMethod {
if ([delegate respondsToSelector:#selector(doSomething)]) {
[delegate doSomething];
}
}
my UIViewController .h
#import "GeneralMethods.h"
#interface view : UIViewController<UIGestureRecognizerDelegate,
UINavigationControllerDelegate,
UIImagePickerControllerDelegate,
myNSObjectClassDelegate>
The error is here at .h - Cannot find protocol declaration for 'myNSObjectClassDelegate'
my UIViewController .m
-(void)doSomething{
}
What am I doing wrong?
EDIT : I figure it out:
For some reason, at my NSObject class, if I'm moving the #import myUIViewcontroller after this:
#protocol myNSObjectClassDelegate <NSObject>
#required
-(void)doSomething;
#end
the problem solved.
We can't see all the code but after reading some more of the exchanges it looks like dependencies are maybe added out of order. I recommend moving the protocol into its own file and #import'ing it to all the places that use it. This way you definitely will be importing things in the order you expect.
In addition to that, the following property declaration:
#property (nonatomic,strong) id<myNSObjectClassDelegate> delegate;
Delegates should be declared as weak, not strong.
#property (nonatomic,weak) id<myNSObjectClassDelegate> delegate;
The reason is to avoid retain cycles/memory leaks. Typically, though not always the relationship looks like this:
Parent Object (usually Controller) -----STRONG-----> Child (View often)
View Thing ----WEAK--------> delegate (actually the Parent Object)
Now if the relationship is STRONG both ways, releasing the parent from all who own it will not be sufficient to release it since its child also holds an owning relationship to it.
Also you can omit the:
#synthesize delegate;
Auto property synthesis renders this obsolete.
Make sure to import your NSObject class .h. file into your UIViewController's .h file (or wherever you declare the protocol). As it stands, you haven't imported the header that declares the protocol so your view controller has no way of knowing that it exists.
Ex:
#import "FileWithProtocol.h"
#interface MyClass <MyProtocol>
...
#end
In the view controller .h file, try adding this line:
#protocol MyNSObjectClassDelegate;
before
#interface ...
I have two UIViewController sublcasses, both of them conform to the UITextFieldDelegate protocol. IOW, I have these classes.
# MyVC1.h
#interface MyVC1 : UIViewController <UITextFieldDelegate>
# MyVC1.m
#interface MyVC1 () {
// Private variable, so not a property
UITextField *_myTextField;
}
#end
#implementation MyVC1
- (void)viewDidLoad
{
_myTextField = [self textFieldwithPlaceHolderText:#"*Text"];
}
#end
SAME CODE for MyVC2 class, except of course the class name.
However, and this is the strange part, my code compiles for MyVC1, but NOT for MyVC2. For MyVC2, compiler says "No visible #interface for "MyVC2" declares the selector "textFieldwithPlaceHolderText". What am I missing for MyVC2? I've double- and triple-checked!
Like Jsdodgers said, textFieldwithPlaceHolderText is not a method of UITextFieldDelegate. Check your #imports section on both controllers - maybe VC1 imports a category for UIViewController class that adds that method to it. A category import would look like this: #import "UIViewController+_.h"
Ok, it's late, and I'm sleepy. MyVC1 defines the textFieldwithPlaceHolderText method, but NOT MyVC2. I cut/past the viewDidLoad section, but forgot to cut/paste the method. Sorry to waste your time.
copy the method "textFieldwithPlaceHolderText" in myVc2.h in interface a
and implement in myvc2.m if i post anything wrong sorry for that
i've worked with delegation before. i know how to create delegation from a superview to a subview class. however, i'm trying to do it the opposite way using the same approach but it's not working! is delegation meant only to work one way or is there a way/trick to use it as a two way communication between the classes? I'm receiving an error at the parent/superview .h class which is:
Cannot find protocol definition for 'SubViewControllerDelegate'
my code goes like this:
subview.h
#import <UIKit/UIKit.h>
#import "SuperViewController.h"
#protocol SubViewControllerDelegate <NSObject>
- (void)someMethod:(NSData *)data;
#end
#interface SubViewController : UIViewController
#property (weak, nonatomic) id <SubViewControllerDelegate> delegate;
#end
subview.m:
[self.delegate someMethod:data];
SuperView.h
#import <UIKit/UIKit.h>
#import "SubViewController.h"
#interface SuperViewController : UIViewController <SubViewControllerDelegate>
#end
SuperView.m:
#pragma mark - SubView Controller Delegate Methods
- (void)someMethod:(NSData *)data{
NSLog(#"%#", data);
}
am i doing anything wrong or missing out anything?
You have an "import-cycle", because "SuperViewController.h" imports "SubViewController.h" and vice versa.
Removing the #import "SuperViewController.h" in "SubViewController.h"
should solve the problem.
If you really need that class to be declared in "SubViewController.h", use
#class SuperViewController; to avoid the import-cycle.
Remark: The <SubViewControllerDelegate> protocol declaration is probably not
needed in the public interface "SuperViewController.h" at all.
In "SuperViewController.h", declare the class as
#interface SuperViewController : UIViewController
In "SuperViewController.m", define a class extension with the protocol:
#interface SuperViewController () <SubViewControllerDelegate>
#end
I want to use a custom method in a custom class in my viewcontroller
//Viewcontroller.h
#import "Class1.h"
//Class1.h
//#import "Class1+Category.h" // Deemed unnecessary in comments below.
#interface Class1: NSObject
-(void)doSomething;
#end
//Class1.m
#import "Class1.h"
#implementation Class1
-(void)doSomething{
NSLog("In doSomething");
}
#end
Now I want a category method of Class1.
//Class1+Category1.h
#import "Class1.h"
#interface Class1 (Category1) // ERROR : Cannot find interface declaration
-(void)doAnotherThing;
#end
//Class1+Category1.m
#import "Class1+Category.h"
#implementation Class1 (Category1)
-(void)doAnotherThing{
NSLog(#"Did Another thing");
}
#end
Finally - in my viewcontroller.m I see the doSomething method, but not the doAnother thing
//viewcontroller.m
Class1 *myClass1 = [[Class1 alloc]init];
[Class1 doSomething]; //Works great!
[Class1 doAnotherThing]; //Not recognized
I have added the -all_load to my target settings. I am out of ideas..do I use the #class? I get 'Cannot find interface declaration' error
Your class and category seems correct at first glance but your controller needs to import Class1+Category.h. Perhaps that's what you missed?