Search code examples
iosprotocolsdelegation

Cannot find protocol declaration for changeMapTyp iOS


I currently get the error "cannot find protocol declaration for changeMapTyp" and I have no idea why. I imported all the required files, but it hasn't solved my problem.

As mentioned in my comments, the error message disappears when I delete the #import MapsViewController.h in the MapsBackgroundViewController.h, but I can´t delete the line, because I need to write this lines

MapsViewController *myMapsViewController = [[MapsViewController alloc] init];
[self setDelegate:myMapsViewController];

to set my delegate. Am I right?

Here is my code:

MapsBackgroundViewcontroller.h

#import "MapsViewController.h"

@protocol ChangeMapTyp <NSObject>
@required
- (void)segmentedControllChangedMapType:(MKMapType) type ;
@end


@interface MapBackgroundViewController : UIViewController{
IBOutlet UISegmentedControl *segmentedControl;
MKMapType mapType;
id < ChangeMapTyp> delegate;

}


@property (nonatomic) IBOutlet UISegmentedControl *segmentedControl;

@property(nonatomic) MKMapType mapType;

@property(nonatomic)id delegate;

- (IBAction)segmentedControllChanged:(id)sender;

MapsBackgroundViewController.m

#import "MapBackgroundViewController.h"

@interface MapBackgroundViewController ()

@end

@implementation MapBackgroundViewController
@synthesize segmentedControl, mapType, delegate;


- (void)viewDidLoad
{
    [super viewDidLoad];

    // that´s why I can´t delete the import ???
    MapsViewController *myMapsViewController = [[MapsViewController alloc] init]; 
    [self setDelegate:myMapsViewController];    
 }


- (IBAction)segmentedControllChanged:(id)sender {

    if (segmentedControl.selectedSegmentIndex == 0) {
        mapType = MKMapTypeStandard;
    }else if (segmentedControl.selectedSegmentIndex == 1) {
        mapType = MKMapTypeSatellite;
    } else if (segmentedControl.selectedSegmentIndex == 2) {
     //   [self.delegate setMapType:MKMapTypeHybrid];
        mapType = MKMapTypeHybrid;
    }


    //Is anyone listening
    if([delegate respondsToSelector:@selector(segmentedControllChangedMapType:)])
    {
        //send the delegate function with the amount entered by the user
        [delegate segmentedControllChangedMapType:mapType];
    }  
    [self dismissModalViewControllerAnimated:YES];    
}

MapsViewController.h

#import "MapBackgroundViewController.h"

@class MapBackgroundViewController;

@interface MapsViewController : UIViewController<MKMapViewDelegate,
UISearchBarDelegate,   ChangeMapTyp >{        //here i get the error message
@private
IBOutlet MKMapView *map;

}

@property (nonatomic, retain) IBOutlet MKMapView *map;

@end

MapsViewController.m #import "MapBackgroundViewController.h"

@interface MapsViewController ()

@end

@implementation MapsViewController

@synthesize map;

- (void)segmentedControllChangedMapType: (MKMapType) type{
    map.mapType = type;   
}

Solution

  • In MapsViewController.h, you need to have -

    #import "MapBackgroundViewController.h" //Keep This line
    
    @class MapBackgroundViewController;   // Remove this line
    
    @interface MapsViewController : UIViewController<MKMapViewDelegate,UISearchBarDelegate,   ChangeMapTyp >{     
    

    Hope this will fix your issue.