Passing UISegmentedControl values from FlipSideViewController in an Utility application to the mainviewcontroller…

只谈情不闲聊 提交于 2019-12-06 15:09:45

问题


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!



来源:https://stackoverflow.com/questions/4717321/passing-uisegmentedcontrol-values-from-flipsideviewcontroller-in-an-utility-appl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!