ios - 代表没有回应

标签 ios delegation respondstoselector

我正在尝试在两个 Viewcontroller 之间使用委托(delegate),但不幸的是我的委托(delegate)没有被解雇。我希望有人可以帮助我解决问题。我有一个名为 MapBackgroundViewController 的 ViewContoller 和一个名为 MapsViewController。如果 MapsBackgroundViewController 的 SegmentedControl 发生变化,应通知 MapsViewController。
(我实际上尝试在 iPhone 上用 patrial curl 实现类似 map 应用的东西)

这是我的代码的一部分:

MapBackgroundViewController.h

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


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

}


@property IBOutlet UISegmentedControl *segmentedControl;

@property MKMapType mapType;

@property(strong)id delegate;

- (IBAction)segmentedControllChanged:(id)sender;

MapBackgroundViewController.m
@interface MapBackgroundViewController ()

@end

@implementation MapBackgroundViewController
@synthesize segmentedControl, mapType, delegate;


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

[self setDelegate:self];

NSLog(@"%@",self.delegate);

}


- (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

NSLog(@"%@",self.delegate);

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

[self dismissModalViewControllerAnimated:YES];

}

MapsViewController.h
#import "MapBackgroundViewController.h"

@class MapBackgroundViewController;

@interface MapsViewController : UIViewController<MKMapViewDelegate,
UISearchBarDelegate,  ChangeMapTyp >{
 IBOutlet MKMapView *map;
}


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


@end

MapsViewController.m(以下方法由于某种原因从未被调用)
 - (void)segmentedControllChangedMapType: (MKMapType) type{
    map.mapType = type;
 }

最佳答案

MapBackgroundViewController您已设置 delegate属性(property)给 self ,所以代表是self - ( MapBackgroundViewController ) 所以当你执行检查时 if([self.delegate respondsToSelector:@selector(segmentedControllChangedMapType:)])它返回 NO , 因为 self.delegate (即 self ,即 MapBackgroundViewController ),没有实现此方法。如果你想要你的 MapsViewController成为代表,在您的MapBackgroundViewController您必须有 MapsViewController 的实例(例如称为 myMapsViewController)然后设置 self.delegate = myMapsViewController .

关于ios - 代表没有回应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10672973/

相关文章:

ios - respondsToSelector 在 UIButton 上为 "setTitle:"返回 YES 但没有这样的选择器

ios - App Store 禁止 performSelector 和 respondsToSelector 吗?

ios - respondsToSelector 检查后出现 doesNotRecognizeSelector 错误

ios - 应用更新后的 Apple 搜索广告

ios - 在 Swift 中为 "CropRect"设置一个固定的宽度和高度

ios - 如何使用swift以编程方式获取设备IMEI号

ruby - 奇怪的 SimpleDelegator 行为

ios - iOS 9 中的 NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9802)

ios - 使用委托(delegate)清理数组

oop - 关于授权