【问题标题】:How to send information from child to parent in UINavigationBar (Unwind segue issue)?如何在 UINavigationBar 中从子级向父级发送信息(Unwind segue 问题)?
【发布时间】:2015-08-27 17:50:14
【问题描述】:

我有一个从 VC1 到 VC2 的导航控制器。在 VC2 中,我有一个 mapkit 视图。在 VC2 用户中搜索位置。当用户单击任何注释时,它将返回到 VC1。现在我也想将注释中的地址发送回 VC1。换句话说,我想将数据从 VC2 传递到 VC1。我见过放松赛格。正如我在教程中看到的那样,我需要 Ctrl+拖动该项目才能退出。我的问题是我无权访问该元素。这个触发回到 VC1 的元素是动态的。
如果有人可以给我一个以编程方式执行此操作或任何其他建议的示例,我将不胜感激。

**EDIT**
This element is an annotation, which is dynamic. 



- (IBAction)searchLocation:(id)sender {

    // Create and initialize a search request object.
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = _nameLocation.text;
    request.region = _mapview.region;

    // Create and initialize a search object.
    MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];

    // Start the search and display the results as annotations on the map.
    [search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error)
    {
        NSMutableArray *placemarks = [NSMutableArray array];

        for (MKMapItem *item in response.mapItems) {
            [placemarks addObject:item.placemark];
        }

        [_mapview removeAnnotations:[_mapview annotations]];
        [_mapview showAnnotations:placemarks animated:NO];
    }];
    NSString*  nameCurrLocation = _nameLocation.text;
    NSLog(@"Location is %@",nameCurrLocation);
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

    MKPinAnnotationView *pinAnnotation = nil;
    if(annotation != mapView.userLocation)
    {
        static NSString *defaultPinID = @"myPin";
        pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinAnnotation == nil )
            pinAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        pinAnnotation.canShowCallout = YES;

        //instatiate a detail-disclosure button and set it to appear on right side of annotation
        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinAnnotation.rightCalloutAccessoryView = infoButton;

    }
    return pinAnnotation;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    static NSString *defaultPinID = @"myPin";
    MKPinAnnotationView *pinAnnotation = nil;
    pinAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:view.annotation reuseIdentifier:defaultPinID];
    NSLog(@"Text got clicked %@",view.annotation.title);
    //From here I want to send view.annotation.title to VC1**
}

【问题讨论】:

  • "这个触发回到 VC1 的元素是动态的",这是什么意思?
  • @Poql Item 是 mapkitview 上的图钉。用户搜索位置后,他们会在地图上找到图钉。

标签: ios


【解决方案1】:

您可以使用代码执行转场。

Ctrl + 拖动你的 VC1 到 VC2。在你的 segue 中给出一个标识符。

在代码中,检测引脚何时被点击,然后使用控制器的 performSegueWithIdentifier:sender: 方法推送控制器并覆盖方法 prepareForSegue:sender: `

【讨论】:

    【解决方案2】:

    您可以有多种方法来实现这一点,例如通过委托、通知或将父视图的实例传递给呈现的视图。

    通过委托:

    @interface VC1: UIViewController<VC2Delegate>
    @property(strong,nonatomic) NSString *changeMe;
    @end
    
    @implementation VC1: UIViewController
    -(void) showVC2 {
        VC2 *vc2 = [[VC2 alloc] init];
        vc2.delegate = self;
        [navigationController presentViewController:vc2 animated:YES completion:nil];
    }
    -(void)changeVariable:(NSString*) newValue {
        self.changeMe = newValue;
    }
    @end
    
    @protocol VC2Delegate <NSObject>
    -(void)changeVariable:(NSString*) newValue;
    @end
    @interface VC2: UIViewController
    @property(strong,nonatomic) <id>VC2Delegate delegate;
    @end
    
    @implementation VC2: UIViewController
    -(void) annotationClicked:(id) sender {
        [self.delegate changeVariable:@"Foo!"];
    }        
    @end
    

    通过通知:

    @interface VC1: UIViewController
    @property(strong,nonatomic) NSString *changeMe;
    @end
    
    @implementation VC1: UIViewController
    -(void) viewDidload {
        [super viewDidLoad];
    
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:@"ChangeMe"
                                                  object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changeTheValue:)
                                                 name:@"ChangeMe"
                                               object:nil];
    }
    -(void) changeTheValue:(id) sender
    {
        NSString *newValue = [sender userInfo][@"newValue"];
        changeMe = newValue;
    }
    
    -(void) showVC2 {
        VC2 *vc2 = [[VC2 alloc] init];
        [navigationController presentViewController:vc2 animated:YES completion:nil];
    }
    @end
    
    @interface VC2: UIViewController
    
    @end
    
    @implementation VC2: UIViewController
    -(void) annotationClicked:(id) sender {
        [[NSNotificationCenter defaultCenter]  postNotificationName:@"ChangeMe"
                                                            object:nil
                                                          userInfo:@{@"newValue":@"Foo!"}];
    
    }        
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      • 2019-02-22
      • 2019-08-22
      • 2021-04-16
      • 2015-08-03
      • 1970-01-01
      • 2021-05-27
      相关资源
      最近更新 更多