将UISegmentedControl值从Utility应用程序中的FlipSideViewController传递到mainviewcontroller ...

时间:2011-01-17 20:04:49

标签: iphone ios4 uisegmentedcontrol application-settings

例如,如果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回答的那样)......?

1 个答案:

答案 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!