cannot find protocol declaration custom protocol delegate iphone - iphone

Slowly but surely getting this delegation and protocol stuff on iphone but I cannot understand this error.
I have declared my protocol in my first viewcontroller.
In the second viewcontroller i try to add it at the top after i have imported it into the header file and it cannot find it. see my code below.
//SendSMS
#import <UIKit/UIKit.h>
#import "LoginPage.h"
#import "MessageOptions.h"
#protocol SMSProtocol <NSObject>
-(NSString *)postbackType;
#end
#interface SendSMS : UIViewController <UITextViewDelegate, UITextFieldDelegate> {
id<SMSProtocol> delegate;
MessageOptions *messageOptions;
LoginPage *loginPage;
IBOutlet UITextField *phonenumber;
IBOutlet UITextView *smsBody;
IBOutlet UIScrollView *scrollview;
}
#property (nonatomic, retain) id<SMSProtocol> delegate;
-(IBAction)LoadMessageOptions;
#end
Then my second view
#import <UIKit/UIKit.h>
#import "SendSMS.h"
#interface ScheduledSMS : UIViewController <SMSProtocol>{
}
-(IBAction)popBack;
#end

That is surely strange. Have you tried restarting Xcode? Xcode has a habit of not indexing symbols for me when I add new files.
You should also look into how your naming conventions. SendSMS is not really a good class name, more of a action method name. I would go for SendSMSViewController, since that is what it is.
By that it would follow that SMSProtocol should be named SendSMSViewControllerDelegate, since that is what it is.
Methods in a delegate protocol should contain the sender and one of the three words will, did, or should. If not at the very least it should name what it expects to return. -(NSString *)postbackType; should probably be -(NSString *)postbackTypeForSendSMSViewController:(SendSMSViewController*)controller;.

Related

Missing '#end' // #end must appear in an objective-c context

This is my code:
#import <UIKit/UIKit.h>
#interface CustomCellArticle: UITableViewCell
#property(nonatomic,retain) IBOutlet UILabel *name;
#end
In first time I received this error:
Missing #end
Expected identifier or '('
in the first of the code, and it required me to add #end in the first to fix it.
the code became like this:
#import <UIKit/UIKit.h>
#end //here the seconde error
#interface CustomCellArticle: UITableViewCell
#property(nonatomic,retain) IBOutlet UILabel *name;
#end
When I add it, I received a new error:
#end must appear in an Objective-C context
I don't know what's happened exactly, please help!
I used the same class in another project and it works fine!
Yes that is from a another header or implementation file already imported beforehand that is missing a #end
It could be a .h or .m file
#end should only come once in a single file.Whats with the top #end.And import all files at the top ?
you may have opened a "{" that is never closed with a "}" before the #end line...
so, the error is not at the #end line... but xcode just find out you are missing a "}" or ")"
Let me show whole file .h and .m file then i can answer well No Problem.
You need to remove first #end from .h file and run you will solve the issue.
Instead of this :
#import <UIKit/UIKit.h>
#end
#interface CustomCellArticle: UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *name;
#end
Use this :
#import <UIKit/UIKit.h>
#interface CustomCellArticle: UITableViewCell
{
}
#property (strong, nonatomic) IBOutlet UILabel *name;
#end
May this will help you. This is working fine for me.
I have checked also in xcode. You Just need to remove first #end.
If this thing is not working you have some another issue.

Defining delegate function in separate file (instead of in ViewController)

I have a ViewController, and a UIView.
The UIView has a delegate, and the delegate function is set in the ViewController.
All I want to do, is have the delegate function defined in a separate file. So the UIView.m #imports the separate file, instead of all the ViewControllers which use the UIView.
I believe this is a standard procedure, but keep falling over myself trying to get it to work. :| Would really appreciate some help. Thanks.
myViewController.h
#import <UIKit/UIKit.h>
#import "myUIView.h"
#protocol ModalViewDelegate
-(void)didReceiveMessage:(NSString *)message;
#end
#interface myViewController : UIViewController <ModalViewDelegate>
#property (nonatomic, strong) myUIView *myUIViewItem;
#end
myViewController.m
#import "myViewController.h"
#import "myUIView.h"
#interface myViewController ()
#end
#implementation myViewController
#synthesize myUIViewItem;
- (void)didReceiveMessage:(NSString *)message { //<<< THIS IS WHAT
NSLog(#"Message from button: %#", message); //<<< NEEDS MOVING
}
- (void)viewDidLoad
{
…
myUIViewItem.delegate = self;
…
myUIView.h
#import <UIKit/UIKit.h>
#protocol ModalViewDelegate;
#interface myUIView : UIView {
id<ModalViewDelegate> delegate;
}
#property (nonatomic, strong) id<ModalViewDelegate> delegate;
myUIView.m
#import "myUIView.h"
#import "myViewController.h"
#implementation myUIView
#synthesize delegate;
...
[delegate didReceiveMessage:#"Data from UIView!"];
well, there is one method actually,
Just take one .h file and lets say connectionDelegate.h and declare your protocol init
In connectionDelegate.h:
#import <UIKit/UIKit.h>
#protocol ConnectionDelegate
-(void)getResult:(NSString*)_result;
#end
Then in your view controller:
#import "ConnectionDelegate.h"
#interface myViewController : UIViewController <ConnectionDelegate>
{
id delegate;
}
then in .m file, by just call the method
[delegate getResult:_result];
Edit regarding the warnings:
You need to declare the method in view controller, you need to do like this.
[self getResult:urlDataString];
-(void)getResult:(NSString*)_result{
[delegate getResult:_result];
}
Based on your comment:
I want to have the function 'didReceiveMessage' defined in a SEPARATE
file. So that I don't have to repeat it in every ViewController that
uses the UIView and delegate. e.g. ModalViewDelegate_Action.h and
ModalViewDelegate_Action.m
The way I was given was to use a subclass, and that's been working great for me. In my iOS projects I have a class called BaseViewController, which is a subclass of UIViewController. I put lots of code in it related to HUD management, NSOperations management, etc. Then virtually all my view controllers are subclasses of it.

Duplicate protocol definition

Getting warning message that Duplicate protocol definition of ModalViewDelegate is ignored
Defined protocol in modalviewcontroller.h file
#protocol ModalViewDelegate;
-(void)dismissView:(id)sender;
#interface Modalviewcontroller : UIViewController
{
id<ModalViewDelegate>delegate;
}
#property (nonatomic, assign) id<ModalViewDelegate>delegate;
#end
In the Modalviewcontroller.m file synthesize delegate
In Mainviewcontroller.h file
#protocol ModalViewDelegate
-(void)didDismissModal:(id)sender;
#end
#interface Mainviewcontrollerontroller : UIViewController <ModalViewDelegate>
-(void)showModal:(id)sender;
In the Mainviewcontroller.m not synthesize delegate
Am I supposed to delegate in mainviewcontroller.m file too?
Why I'm getting warning message of duplicate protocol definition?
Try to remove #protocol ModalViewDelegate; in modalviewcontroller.h and import Mainviewcontroller.h in this file.
You are defining the protocol twice one in mainviewcontroller.h and the other in modalViewController.h...thats why you are getting the warning...

error instantiating other view controllers

I am sure this is really dumb, but I just cant seem to understand why am I getting this error. In my project I have a view controller and another class that does some data structuring job (not relevant anyway). I am getting a compilation error: "unknown type name "the view controller"" when trying to instantiate it within my class.
This is my class .h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import "MyLocationController.h"
#import "GetZip.h"
#import "SecondTab.h"
#interface DataEngine : NSObject <MyLocationControllerDelegate, MKMapViewDelegate, GetZipcodeDelegate> {
MyLocationController *CLController;
GetZip *getzip;
SecondTab *secondTab; //ERROR IS HERE
}
My view controller .h:
#import <UIKit/UIKit.h>
#import "FirstTab.h"
#import "DataEngine.h"
#interface SecondTab : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *table1;
NSString *address;
NSDate *time;
NSDictionary *dataDict;
DataEngine *fullData;
}
(I omitted all the #synthesis since I dont think they matter...in any case, I do #property (nonatomic, retain) for everything).
Any idea what can go wrong here?
Why don't you try to forward declare it. Use #class secondTab instead of #import secondTab? It will help avoiding any circular inclusions if thats the problem.

Objective C Properties

I tried searching for this issue, but can't seem to find out what I'm doing wrong.
Here is my controller header:
#import <UIKit/UIKit.h>
#interface BabyLearnViewController : UIViewController {
UIButton *btnImage;
MediaManager* myMediaManager;
}
#property (nonatomic, retain) IBOutlet UIButton *btnImage;
#property (retain) MediaManager* myMediaManager;
- (IBAction)setNewImage;
#end
Here is my controller class:
#import "BabyLearnViewController.h"
#import "MediaManager.h";
#implementation BabyLearnViewController
#synthesize btnImage;
#synthesize myMediaManager;
I am getting the errors:
error: expected specifier-qualifier-list before 'MediaManager'
error: no declaration of property 'myMediaManager' found in the interface
Any ideas? Usually the 1st error comes up if you have a cylical reference. 'MediaManager' doesn't reference anything else. Any ideas?
Since you have no mentions of MediaManager class at the moment it is used in header file compiler can't figure out what "MediaManager" is and issues an error. Declare that class using forward declaration in your header file to let compiler know that MediaManager is actually a class:
#class MediaManager;
#interface BabyLearnViewController : UIViewController {
...
P.S. As an alternative you can import MediaManager.h in your header, but using forward declaration is preferred.
Place #import "MediaManager.h"
this in header file of BabyLearnViewController
try add #class MediaManager; before #interface