OhDoh
OhDoh

Reputation: 433

Delegates issues

I can't get why I can't set delegate. I'm using UINavigationController to switch between two views. Here is my code

SecondViewProtocol.h

#import <Foundation/Foundation.h>

@protocol SecondViewProtocol <NSObject>
    @required
    -(void)textFieldDidChange:(NSString *)data;
@end

SecondView.h

#import <UIKit/UIKit.h>
#import "SecondViewProtocol.h"
@interface SecondView : UIViewController

@property (nonatomic, retain) id<SecondViewProtocol>delegate;
@end

SecondView.m

@synthesize delegate = _delegate;
.......

-(IBAction)textFieldReturn:(id)sender
{
    [[self delegate] textFieldDidChange:[self.textField text]];
}

.......

I have imported SecondViewProtocol.h in FirstView.h

FirstView.m

....
SecondView *secondView = [[SecondView alloc]init];
secondView.delegate = self;
....

Here I get Assigning to id from incompatible type FirtView.

What is wrong here ?

Upvotes: 0

Views: 115

Answers (1)

calinb
calinb

Reputation: 215

First of all, delegate property should be declared as assign, not retain. You should never retain delegates. Second, FirstView should conform to SecondViewProtocol like the following.

@interface FirstView: UIViewController <SecondViewProtocol>

Upvotes: 1

Related Questions