不执行协议方法中的代码

时间:2014-12-19 19:48:35

标签: ios objective-c

我是ios开发的新手,我正在创建一个带有2个视图控制器的应用程序。我使用Protocols和Delegates将数据从一个控制器发送到预览控制器。数据传递正常。我的问题是代码是在控制器中实现的协议的方法中,获取数据的方法不执行。方法中的NSLOG是执行但其余的gode没有。 有没有想过为什么会这样?

发送数据的控制器代码:

·H

@class GeoCoding_controller;

@protocol GeoContDelegate <NSObject>

-(void)addItemViewController:(GeoCoding_controller *)controller didFinishEnteringItem:     (NSString *)item;
 @end

的.m

   NSString *itemToPassBack = [NSString stringWithFormat:@"%@,%@,%@",final_loc[indexPath.row][@"form_address"],final_loc[indexPath.row][@"lat"],final_loc[indexPath.row][@"lng"]];;

   First_view_controller *first_cont = [self .storyboard instantiateViewControllerWithIdentifier:@"first_view"];

[self presentViewController:first_cont animated:YES completion:nil];

[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];

控制器中获取数据的代码:

·H

@interface First_view_controller : UIViewController<CLLocationManagerDelegate,GMSMapViewDelegate,GeoContDelegate>

的.m

   -(void)addItemViewController:(GeoCoding_controller *)controller didFinishEnteringItem:(NSString *)item{


double delay = 10.0;

dispatch_time_t poptime = dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC);

dispatch_after(poptime, dispatch_get_main_queue(), ^{


        NSArray *foo = [item componentsSeparatedByString:@","];

        NSString *address = [NSString stringWithFormat:@"%@ %@ %@",[foo objectAtIndex:0],[foo objectAtIndex:1],[foo objectAtIndex:2]];

        double lat = [[foo objectAtIndex:3]doubleValue];

        double lng =[[foo objectAtIndex:4] doubleValue];


        GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lat longitude:lng zoom:18];

        MAP.camera = camera;

        edtaddress.text = address;



        NSLog(@"data from controller : %@", item);

    });

}

1 个答案:

答案 0 :(得分:1)

你没有为自己设置代表,你应该这样做:

self.delegate = first_cont;

First_view_controller *first_cont = [self .storyboard instantiateViewControllerWithIdentifier:@"first_view"];