1

我正在尝试在两个 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;
 }
4

1 回答 1

6

MapBackgroundViewController您已将delegate属性设置为self,因此委托是self- ( MapBackgroundViewController) 因此,当您执行检查时,if([self.delegate respondsToSelector:@selector(segmentedControllChangedMapType:)])它返回NO,因为self.delegate(即self,即MapBackgroundViewController)未实现此方法。如果你想让你MapsViewController成为代表,MapBackgroundViewController你必须有一个实例MapsViewController(例如调用myMapsViewController),然后设置self.delegate = myMapsViewController.

于 2012-05-20T11:52:28.467 回答