例如,如果flipsideviewcontroller中的segmentedcontrol发生变化,我希望Mainviewcontroller中的maptype从satellite变为hybrid?我究竟做错了什么?基本上,我希望mainviewcontroller对flipsideviewcontroller中的更改做出反应!!!
FlipsideViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKMapView.h>
@protocol FlipsideViewControllerDelegate;
@interface FlipsideViewController : UIViewController {
id <FlipsideViewControllerDelegate> delegate;
IBOutlet UISegmentedControl *mapType_;
}
@property (nonatomic, retain) UISegmentedControl *mapType_;
@end
MainViewController.h
@interface MainViewController : UIViewController <XXXX> {
IBOutlet UISegmentedControl *mapType;
}
@property (nonatomic, retain) UISegmentedControl *mapType;
@end
MainViewController.m
-(void)viewDidLoad {
if(mapType.selectedSegmentIndex==0){
mapView.mapType=MKMapTypeStandard;
}
else if (mapType.selectedSegmentIndex==1){
mapView.mapType=MKMapTypeSatellite;
}
else if (mapType.selectedSegmentIndex==2) {
mapView.mapType = MKMapTypeHybrid;
}
}
有关如何实现这一目标的任何想法?我究竟做错了什么?真的很感激答案!谢谢!
如何实现委托方法(正如phix23回答的那样)......?
答案 0 :(得分:4)
(1)通过这种新方法扩展FlipsideViewControllerDelegate
协议:
-(void)flipsideViewControllerSelectionChangedToIndex:(int)index;
(2)在FlipsideViewController
中添加IBAction以便重新分配分段控件的ValueChanged-Event:
-(IBAction) valueChanged {
[delegate flipsideViewControllerSelectionChangedToIndex: mapType_.selectedSegmentIndex];
}
(3)在MainViewController
中实现委托方法:
-(void)flipsideViewControllerSelectionChangedToIndex:(int)index {
if (index == 0) mapView.mapType = MKMapTypeStandard;
if (index == 1) mapView.mapType = MKMapTypeSatellite;
if (index == 2) mapView.mapType = MKMapTypeHybrid;
}
并删除MainViewController
中的IBOutlet!