protocol problem

Discussion in 'iOS Programming' started by nashyo, Jan 14, 2012.

  1. nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    I'm receiving error "Cannot find protocol declaration for CheckmarkDelegate" in ContentAViewController.h. Why is this?

    QuizTableViewController.h
    Code:
    #import <UIKit/UIKit.h>
    #import "ContentAViewController.h"
    
    @protocol CheckmarkDelegate <NSObject>
    @required
    - (void) processSuccessful: (BOOL)success;
    
    @end
    
    @interface QuizTableViewController : UITableViewController
    {
        id <CheckmarkDelegate> delegate;
    }
    
    @property (retain) id delegate;
    
    @end
    QuizTableViewController.m
    Code:
    #import "QuizTableViewController.h"
    
    @implementation QuizTableViewController
    @synthesize delegate;
    
    - (void) processComplete
    {
        [self.delegate processSuccessful:YES];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self.tableView reloadData];
    }
    ContentAViewController.h
    Code:
    #import <UIKit/UIKit.h>
    #import "QuizTableViewController.h"
    
    @interface ContentAViewController : UIViewController <CheckmarkDelegate>
    
    @end
    ContentAViewController.m
    Code:
    #import "ContentAViewController.h"
    
    @implementation ContentAViewController
    
    - (void) processSuccessful: (BOOL) success
    {
        NSLog(@"Process Succesful");
    }
    
    - (IBAction)OK:(id)sender 
    {
        if ([answerAVal.text isEqualToString:@"CORRECT!"])
        {
            [self processSuccessful:YES];
        } else {
            
        [self dismissModalViewControllerAnimated:YES];
        }
    }
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Because QuizTableViewController.h is importing ContentAViewController.h.


    Think about what happens when the compiler comes to compiling QuizTableViewController.m.

    The first thing it encounters is #import "QuizTableViewController.h". So basically, that line is replaced with the contents of QuizTableViewController.h and compilation effectively continues with the first line of QuizTableViewController.h.

    The second thing that happens is UIKit/Kit.h is imported in the same way.

    The third thing that happens is #import "ContentAViewController.h" again in the same way.

    Effectively we're now at the first line of "ContentAViewController.h. The #import <UIKit/UIKit.h> does nothing because UIKit/UIKIt.h has already been imported.

    The #import "QuizTableViewController.h" is also ignored because QuizTableViewController.h has already (is being) imported.

    Then the compiler get to the @interface ContentAViewController line in QuizTableViewController.h which references CheckmarkDelegate, but as you can see the compiler has never seen the @protocol CheckmarkDelegate at that point. So it complains.


    For what you've posted, there's not need for QuizTableViewController.h to import ContentAViewController.h, so take it out.
     
  3. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #3
    Confused

    Still doesn't work.
    Getting depressed.
     

Share This Page