-1

I am new to Objective C and trying to grasp the concept of a delegate. I have the following code and have made comments where a warning and an error occur with the build.I am using the latest version of Xcode and using a Storyboard.

From MainViewController.h

#import <UIKit/UIKit.h>
#import "FinalViewController.h"

@class MainViewController;
@protocol MainViewControllerDelegate <NSObject>
- (void)sayHello;
@end

@interface MainViewController : UIViewController
@property (nonatomic, strong) id <MainViewControllerDelegate> delegate;
- (void)writeToMe;
. . .
@end

From MainViewController.m

#import "MainViewController.h"
#import "FinalViewController.h"
@interface MainViewController ()
@end

@implementation MainViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)writeToMe
{
    [self.delegate sayHello];
}
. . .
@end

From FinalViewController.h

#import <UIKit/UIKit.h>
#import "MainViewController.h"

@interface FinalViewController : UIViewController <NSCoding, MainViewController> //The ERROR below appears
    //ERROR:Cannot find protocol declaration for ‘MainViewControllerDelegate’; did you mean ‘UIPageViewControllerDelegate’?
    . . .
@end

From FinalViewController.m

#import "FinalViewController.h"
@interface FinalViewController ()
@end

@implementation FinalViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    MainViewController *mainVC;
    mainVC.delegate = self; //causes WARNING below
    //WARNING:Assigning to ‘id <MainViewControllerDelegate> from incompatible type ‘FinalViewController *const__strong’

    [mainVC writeToMe];
    . . .
}

- (void)sayHello
{
    NSLog(@"sayHello called");
}
@end

I have researched problems and responses to a similar problem on this web site, but have not found a solution using Storyboard.

Jim Botts
  • 59
  • 1
  • 10

2 Answers2

0

Change:

#import "MainViewController.h"
#import "FinalViewController.h"
@interface MainViewController ()
@end

@implementation MainViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)writeToMe
{
    [self.delegate sayHello];
}
. . .

TO:

#import "MainViewController.h"
@interface MainViewController ()
@end

@implementation MainViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)writeToMe
{
    [self.delegate sayHello];
}
. . .

ALSO:

#import <UIKit/UIKit.h>
#import "FinalViewController.h"

@class MainViewController;
@protocol MainViewControllerDelegate <NSObject>
- (void)sayHello;
@end

@interface MainViewController : UIViewController
@property (nonatomic, strong) id <MainViewControllerDelegate> delegate;
- (void)writeToMe;
. . .
@end

TO

#import <UIKit/UIKit.h>


@protocol MainViewControllerDelegate <NSObject>
- (void)sayHello;
@end

@interface MainViewController : UIViewController
@property (nonatomic, weak) id <MainViewControllerDelegate> delegate;
- (void)writeToMe;
. . .
@end

--------------------------- EDIT ------------------------------------

Change

@interface FinalViewController : UIViewController <NSCoding, MainViewController>

to

@interface FinalViewController : UIViewController <NSCoding, MainViewControllerDelegate>
CW0007007
  • 5,681
  • 4
  • 26
  • 31
  • CW0007007: I changed code as you recommended and still get the same error. – Jim Botts Mar 26 '14 at 17:35
  • CW0007007: The Build is successful, however the NSLog statement does not appear in the console. – Jim Botts Mar 26 '14 at 17:41
  • See my answer here on how to properly setup a delegate: http://stackoverflow.com/questions/21331523/how-to-declare-events-and-delegates-in-objective-c/21331771#21331771 – CW0007007 Mar 27 '14 at 08:32
0

This might be a typo but:

@interface FinalViewController : UIViewController <NSCoding, MainViewController>

should be:

@interface FinalViewController : UIViewController <NSCoding, MainViewControllerDelegate>

Also remove the import of "FinalViewController.h" from MainViewController.h.

And your other warning can be fixed by changing:

@interface FinalViewController ()

to:

@interface FinalViewController () < MFMailComposeViewControllerDelegate>

and change:

mainVC.delegate = self;

to:

mainVC.mailComposeDelegate = self;
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • maddy: Deletion of the FinalViewController.h imports resulted in a successful build, but the protocol method was not called. Adding < MFMailComposeViewControllerDelegate> resulted in an Error stating that the protocol could not be located. – Jim Botts Mar 26 '14 at 17:56