0

我试图在两个视图之间传递一个整数。当我从 View1 返回到 MainView 时,该值未传递,并且 NSLog(@"Changed map") 未显示在控制台中。我寻找答案并尝试了所有方法,但它不起作用!感觉代言人有问题……

有任何想法吗?

View1.h

@class FlipsideViewController;

@protocol FlipsideViewControllerDelegate
    - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
    -(void)flipsideViewControllerSelectionChangedToIndex:(int)index;
    @end

@interface FlipsideViewController : UIViewController {
}

@property (weak, nonatomic) id <FlipsideViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UISegmentedControl *mapType;

- (IBAction)valueChangedMap:(id)sender;

@end

查看1.m

-(IBAction)valueChangedMap:(id)sender {
    [self.delegate flipsideViewControllerSelectionChangedToIndex:[sender selectedSegmentIndex]];
    NSLog (@"%d",[sender selectedSegmentIndex]);

    [self dismissViewControllerAnimated:YES completion:nil];
}

主视图.h

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate, UIPopoverControllerDelegate, MKMapViewDelegate> {}

@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property(weak) id<FlipsideViewControllerDelegate> delegate;

@end

主视图.m

-(void)flipsideViewControllerSelectionChangedToIndex:(int)index {
    NSLog(@"Changed map");

    if (index == 0) mapView.mapType = MKMapTypeStandard;
    if (index == 1) mapView.mapType = MKMapTypeHybrid;
    if (index == 2) mapView.mapType = MKMapTypeSatellite;
}
4

1 回答 1

0

您只需要 view1 上的实际委托属性,而您的 mainView 只需要响应该委托协议。这是您的委托协议应如下所示:

@property(nonatomic,unsafe_unretained) id delegate;

现在在您的代码中,当 mainview 呈现或推送 view1 时,您需要像这样连接委托(伪代码):

view1.delegate = self;

还有一些挑剔......你永远不应该让视图控制器自行关闭,当“完成”委托方法触发时,mainView 应该关闭 view1。此外,您应该始终检查委托在触发之前是否响应了选择器:

if([self.delegate respondsToSelector:@selector(yourselectorname)]) {
    [self.delegate yourselectorname];
}
于 2012-12-15T05:12:00.187 回答