Passing UISegmentedControl values from FlipSideVie

2020-08-09 04:44发布

问题:

For example, I want the maptype in the Mainviewcontroller to change from satellite to hybrid if the segmentedcontrol in the flipsideviewcontroller changes? What am i doing wrong? Basically, I want the mainviewcontroller to react to the changes made in the 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;
        }
    }

Any ideas of how to make this possible? What am i doing wrong? Would really appreciate an answer! Thanks!

How do I implement the delegate method (as phix23 answered)...?

回答1:

(1) Extend theFlipsideViewControllerDelegateprotocol by this new method:

-(void)flipsideViewControllerSelectionChangedToIndex:(int)index;

(2) Add an IBAction in FlipsideViewController in order to resond to the ValueChanged-Event of the segmented control:

-(IBAction) valueChanged {
     [delegate flipsideViewControllerSelectionChangedToIndex: mapType_.selectedSegmentIndex];
}

(3) In MainViewController implement the delegate method:

-(void)flipsideViewControllerSelectionChangedToIndex:(int)index {
    if (index == 0) mapView.mapType = MKMapTypeStandard;
    if (index == 1) mapView.mapType = MKMapTypeSatellite;
    if (index == 2) mapView.mapType = MKMapTypeHybrid;
}

And delete the IBOutlet in MainViewController!