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 - 将照片添加到我的应用程序并存储在核心数据中

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

C++允许 friend 的派生类访问私有(private)嵌套类

ios - 处理此错误的最佳方法? [AVCaptureFigVideoDevice setTorchModeOnWithLevel :error:]: unrecognized selector sent to instance

ios - Strava alamofire token

ios - UiCollectionView 间距问题 - swift

ios - 即使内容的 Storyboard已更新,也不会调用通知内容

ios - 海量父子模式和委托(delegate)模式

Objective-C:为什么在 respondsToSelector: 之前检查 nil?