我正在尝试在两个 Viewcontroller 之间使用委托,但不幸的是我的委托没有被解雇。我希望有人可以帮助我解决问题。我有一个名为 MapBackgroundViewController 的 ViewContoller 和一个名为 MapsViewController。如果 MapsBackgroundViewController 的 SegmentedControl 发生变化,应通知 MapsViewController。(我实际上尝试在 iPhone 上用 patrial curl 实现类似地图应用的东西)
这是我的代码的一部分:
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;
}