使用calloutAccessoryControlTapped Xcode传递数据

时间:2016-11-20 18:28:40

标签: ios objective-c iphone xcode mkmapview

我正在开发Xcode中的应用程序(objective-c)。我的应用程序有一个带有餐馆列表的TableView,当您按下一行时,会打开另一个带有餐厅信息的视图。我正在使用的方法是我将行中的标题发送到新视图,并根据标题加载餐馆的信息。我想使用地图引脚按钮完全相同。我的问题是我正在尝试将TitleLabel发送到其他ViewController,但这不起作用,我不知道为什么。

这是我的地图引脚:RestMapPin.h:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface RestMapPin : NSObject <MKAnnotation> {

    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@end

这是我的RestMappin.m:

#import "RestMapPin.h"

@implementation RestMapPin

@synthesize  coordinate, title, subtitle;

@end

这是我的MapViewController.h:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController {

    MKMapView *mapView;
}

@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButton;

@property (nonatomic, retain) IBOutlet MKMapView *mapView;

-(IBAction)setMap:(id)sender;

@end

这是我的MapViewController.m:

#import "MapViewController.h"
#import "SWRevealViewController.h"
#import "RestMapPin.h"
#import "RestViewController.h"
#import "MainTableViewController.h"

@interface MapViewController ()

@end

@implementation MapViewController

@synthesize mapView;

- (void)viewDidLoad {
    [super viewDidLoad];

    _barButton.target = self.revealViewController;
    _barButton.action = @selector(revealToggle:);

    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

    mapView.delegate = self;

    /*Babelia*/
    MKCoordinateRegion region_Babelia = { {0.0, 0.0}, {0.0, 0.0}};
    region_Babelia.center.latitude = 40.4234778;
    region_Babelia.center.longitude =  -3.686283000000003;
    region_Babelia.span.longitudeDelta = 0.1f;
    region_Babelia.span.latitudeDelta = 0.1f;
    [mapView setRegion:region_Babelia animated:YES];

    RestMapPin *ann_Babelia = [[RestMapPin alloc] init];
    ann_Babelia.title = @"Babelia";
    ann_Babelia.subtitle = @"Barrio de Salamanca";
    ann_Babelia.coordinate = region_Babelia.center;
    [mapView addAnnotation:ann_Babelia];

    /*Bacira*/
    MKCoordinateRegion region_Bacira = { {0.0, 0.0}, {0.0, 0.0}};
    region_Bacira.center.latitude = 40.43375390000001;
    region_Bacira.center.longitude =  -3.699036299999989;
    region_Bacira.span.longitudeDelta = 0.1f;
    region_Bacira.span.latitudeDelta = 0.1f;
    [mapView setRegion:region_Bacira animated:YES];

    RestMapPin *ann_Bacira = [[RestMapPin alloc] init];
    ann_Bacira.title = @"Bacira";
    ann_Bacira.subtitle = @"Chamberí";
    ann_Bacira.coordinate = region_Bacira.center;
    [mapView addAnnotation:ann_Bacira];

    /*Indicador de posicion del mapa (para centrarlo)*/
    MKCoordinateRegion region_posicion = { {0.0, 0.0}, {0.0, 0.0}};
    region_posicion.center.latitude = 40.44934744420573;
    region_posicion.center.longitude =  -3.695504665374756;
    region_posicion.span.longitudeDelta = 0.08f;
    region_posicion.span.latitudeDelta = 0.08f;
    [mapView setRegion:region_posicion animated:YES];
    /*************************************************/
}

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *pinView = nil;
    if(annotation != mapView.userLocation) {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil )
            pinView = [[MKAnnotationView alloc]
                       initWithAnnotation:annotation reuseIdentifier:defaultPinID];


        pinView.canShowCallout = YES;
        pinView.image = [UIImage imageNamed:@"pin2@2x.png"];
    }
    else {
        //[mapView.userLocation setTitle:@"I am here"];
    }


    UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    //if ([[annotation title] isEqualToString:@"Marieta"]) {
    //[pinButton addTarget:self action:@selector(annotationView:) forControlEvents:UIControlEventTouchUpInside];
    //}
    pinView.rightCalloutAccessoryView = pinButton;


    return pinView;
}


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    RestMapPin *selectedAnnotation = view.annotation;
    NSLog(@"Title of selected pin - %@", selectedAnnotation.title);
    // Here you get the title of selected annotation

    //RestViewController *objYourVC = [[RestViewController alloc]init];
    RestViewController *objYourVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MyIdentifier"];


    objYourVC.TitleLabel = selectedAnnotation. title;
    NSLog(@"objYourVC.TitleLabel - %@", objYourVC.TitleLabel);
    //objYourVC.DetailModal.title = selectedAnnotation.title;
    //NSLog(@"objYourVC.TitleLabel - %@", objYourVC.TitleLabel);
    objYourVC.DescriptionLabel = selectedAnnotation. subtitle;
    NSLog(@"objYourVC.DescriptionLabel - %@", objYourVC.DescriptionLabel);


    [self.navigationController pushViewController:objYourVC animated:YES];
}



-(IBAction)setMap:(id)sender {

    switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
        case 0:
            mapView.mapType = MKMapTypeStandard;
            break;
        case 1:
            mapView.mapType = MKMapTypeSatellite;
            break;
        case 2:
            mapView.mapType = MKMapTypeHybrid;
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

然后我将向您展示我想要获取objYourVC.TitleLabel

数据的位置

这是我的RestViewController.h:

#import <UIKit/UIKit.h>
#import "Restaurant.h"
@interface RestViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
@property (strong, nonatomic) IBOutlet UILabel *DescriptionLabel;
@property (strong, nonatomic) IBOutlet UIImageView *ImageView;

//@property (strong, nonatomic) IBOutlet NSArray *DetailModal;

@property (weak, nonatomic) IBOutlet UITextView *phoneTextView;
@property (weak, nonatomic) IBOutlet UIButton *webButton;
@property (weak, nonatomic) IBOutlet UITextView *adressTextView;
@property (weak, nonatomic) IBOutlet UIButton *cartaButton;
@property (weak, nonatomic) IBOutlet UITextView *reviewTextView;
@property (weak, nonatomic) IBOutlet UILabel *updateLabel;

@property (nonatomic, strong) Restaurant *DetailModal;

@end

这是我的RestViewController.m:

#import "RestViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface RestViewController ()

@end

@implementation RestViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *decDate = @"11/2016";
if ([_DetailModal.title isEqualToString:@"Babelia"]) {
        _TitleLabel.text = _DetailModal.title;
        _DescriptionLabel.text = _DetailModal.desc;
        _ImageView.image = [UIImage imageNamed:_DetailModal.image];
        _phoneTextView.text = @"(+34) 918 31 71 79";
        _adressTextView.text = @"\n• Callejón de Puigcerdá, 6, Madrid, España";
        [_webButton setTitle:@"Web" forState:UIControlStateNormal];
        [_webButton addTarget:self action:@selector(openWeb_babelia_Url) forControlEvents:UIControlEventTouchUpInside];
        [_cartaButton setTitle:NSLocalizedString (@"Menu", nil) forState:UIControlStateNormal];
        [_cartaButton addTarget:self action:@selector(openCarta_babelia) forControlEvents:UIControlEventTouchUpInside];
        _reviewTextView.text = NSLocalizedString(@"No reviews available.", nil);
        _updateLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Updated: %@", nil), decDate];
    }

    if ([_DetailModal.title isEqualToString:@"Bacira"]) {
        _TitleLabel.text = _DetailModal.title;
        _DescriptionLabel.text = _DetailModal.desc;
        _ImageView.image = [UIImage imageNamed:_DetailModal.image];
        _phoneTextView.text = @"(+34) 91 866 40 30";
        _adressTextView.text = @"\n• Calle Del Castillo, 16, Madrid, España";
        [_webButton setTitle:@"Web" forState:UIControlStateNormal];
        [_webButton addTarget:self action:@selector(openWeb_bacira_Url) forControlEvents:UIControlEventTouchUpInside];
        [_cartaButton setTitle:NSLocalizedString (@"Menu", nil) forState:UIControlStateNormal];
        [_cartaButton addTarget:self action:@selector(openCarta_bacira) forControlEvents:UIControlEventTouchUpInside];
        _reviewTextView.text = NSLocalizedString(@"No reviews available.", nil);
        _updateLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Updated: %@", nil), decDate];
    }
}

-(void)openWeb_babelia_Url{
    UIApplication *application = [UIApplication sharedApplication];
    NSURL *URL = [NSURL URLWithString:@"http://babeliarestaurante.com/contactar/"];
    [application openURL:URL options:@{} completionHandler:^(BOOL success) {
        if (success) {
            NSLog(@"Opened url");
        }
    }];
}

-(void)openWeb_bacira_Url{
    UIApplication *application = [UIApplication sharedApplication];
    NSURL *URL = [NSURL URLWithString:@"http://www.bacira.es/contacto/4585843222"];
    [application openURL:URL options:@{} completionHandler:^(BOOL success) {
        if (success) {
            NSLog(@"Opened url");
        }
    }];
}

-(void)openCarta_babelia{
    UIApplication *application = [UIApplication sharedApplication];
    NSURL *URL = [NSURL URLWithString:@"http://babeliarestaurante.com/nuestra-cocina/las-cartas/"];
    [application openURL:URL options:@{} completionHandler:^(BOOL success) {
        if (success) {
            NSLog(@"Opened url");
        }
    }];
}

-(void)openCarta_bacira{
    UIApplication *application = [UIApplication sharedApplication];
    NSURL *URL = [NSURL URLWithString:@"http://www.bacira.es/carta/4591994095"];
    [application openURL:URL options:@{} completionHandler:^(BOOL success) {
        if (success) {
            NSLog(@"Opened url");
        }
    }];
}

@end

问题是方法calloutAccessoryControlTapped:效果不佳,因为RestViewController.m没有收到objYourVC.TitleLabel。我该怎么办?

当我运行项目时,objYourVC.TitleLabelMapViewController.m(@“Bacira”)中表现良好,但当项目转到RestViewContoller.m TitleLable时它不好,它显示(@“标签”)。

我知道我的Xcode水平很低,我需要你的帮助。我想每天学习更多。谁能帮我?非常感谢你。

编辑:这是我的Restaurant.h:

#import <Foundation/Foundation.h>

@interface Restaurant : NSObject

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, copy) NSString *image;

- (instancetype)init:(NSString *)title descripiton:(NSString *)description image:(NSString *)image;

@end

这是我的restaurant.m:

#import "Restaurant.h"

@implementation Restaurant

- (instancetype)init:(NSString *)title descripiton:(NSString *)description image:(NSString *)image {
    self = [super init];
    if (self != nil) {
        self.title = title;
        self.desc = description;
        self.image = image;
    }
    return self;
}

@end

1 个答案:

答案 0 :(得分:1)

你不能直接处理IBoutlet,因为它们有弱引用。 o你需要存储一个变量,然后在viewDidLoad中你需要更新相应的IBOutlets。

<强> MapViewController.m

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    RestMapPin *selectedAnnotation = view.annotation;
    NSLog(@"Title of selected pin - %@", selectedAnnotation.title);
    // Here you get the title of selected annotation

    //RestViewController *objYourVC = [[RestViewController alloc]init];
    RestViewController *objYourVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MyIdentifier"];


    objYourVC.DetailModal.pagetitle= selectedAnnotation.title;
    objYourVC.DetailModal.pageDescription = selectedAnnotation.desc;


    [self.navigationController pushViewController:objYourVC animated:YES];
}

<强> RestViewController.h:

#import <UIKit/UIKit.h>
#import "Restaurant.h"
@interface RestViewController : UIViewController

@property (nonatomic, strong) NSString *pageTitle;
@property (nonatomic, strong) NSString *pageDescription ;

@end

<强> RestViewController.m:

#import "RestViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface RestViewController ()

@end

@implementation RestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     _TitleLabel.text = _pageTitle;
     _DescriptionLabel.text = _pageDescription;


}

@end