【问题标题】:Cannot find protocol declaration for changeMapTyp iOS找不到 changeMapTyp iOS 的协议声明
【发布时间】:2012-05-24 23:24:07
【问题描述】:

我目前收到错误“找不到 changeMapTyp 的协议声明”,我不知道为什么。我导入了所有必需的文件,但它并没有解决我的问题。

正如我的cmets中提到的,当我删除MapsBackgroundViewController.h中的#import MapsViewController.h时,错误消息消失了,但是我不能删除该行,因为我需要写这行

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

设置我的委托。我说的对吗?

这是我的代码:

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;   
}

【问题讨论】:

  • 我终于把它修好了。我只需要删除 MapsBackgroundViewController.h 中的 #import MapViewController.h 行。
  • 但现在我没有任何错误消息,但我的 mapType 没有按预期更改。有谁是eda?
  • 现在有什么问题,你能更新你的问题吗?
  • 问题已更新。非常感谢您的帮助

标签: ios protocols delegation


【解决方案1】:

MapsViewController.h,你需要有 -

#import "MapBackgroundViewController.h" //Keep This line

@class MapBackgroundViewController;   // Remove this line

@interface MapsViewController : UIViewController<MKMapViewDelegate,UISearchBarDelegate,   ChangeMapTyp >{     

希望这能解决您的问题。

【讨论】:

  • 不幸的是,这并不能解决问题。但正如我所提到的,如果我删除 MapBackgroundViewcontroller 中的导入,错误就消失了。我也不再需要在 MapBackgroundViewController 中设置我的委托,因为我已经在 prepareforsegue(在 MapsViewController 中)方法中设置了它,如下所示: if ([segue.identifier isEqualToString: @"showMapBackground"]) { MapBackgroundViewController *myCtrl = segue.destinationViewController ; myCtrl.delegate = 自我; } 所以,问题解决了。
猜你喜欢
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多