2016-11-20 6 views
0

私はXcode(objective-c)でアプリを開発しています。私のアプリはレストランのリストを持つTableViewを持っていて、1つの行を押すとレストラン情報が表示されます。私が使っている方法は、行のタイトルを新しいビューに送り、タイトルに応じてレストランの情報をロードすることです。 地図ピンボタンを使ってまったく同じことをしたい。私はピンが付いたマップを持っていて、mapPinのタイトルに応じて、レストランの情報を含む別のビューを右の詳細ボタンを押して開きたいと思っています。mapPinボタンを使用して新しいビューを開くXcode

誰かが私を助けることができます!私はチュートリアルや他の投稿を探していたが、私の場合はうまくいきません。どうもありがとうございました!

これは私の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" 

@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]; 

    [self.navigationItem setTitle:NSLocalizedString (@"Map", nil)]; /*Cambia el titulo del navigation controller*/ 

    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; /*Cambia el color de las letras del navigation controller bar del menu principal*/ 

    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:27/255.0f green:101/255.0f blue:163/255.0f alpha:1.0f]]; 

    self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; /*Cambia el color del boton de la izquierda*/ 

    mapView.delegate = self; 

    //1 
    //80 Grados 
    MKCoordinateRegion region_80_grados = { {0.0, 0.0}, {0.0, 0.0}}; 
    region_80_grados.center.latitude = 40.42871179999999; 
    region_80_grados.center.longitude = -3.703639100000032; 
    region_80_grados.span.longitudeDelta = 0.1f; 
    region_80_grados.span.latitudeDelta = 0.1f; 
    [mapView setRegion:region_80_grados animated:YES]; 

    RestMapPin *ann_80_grados = [[RestMapPin alloc] init]; 
    ann_80_grados.title = @"80 Grados"; 
    ann_80_grados.subtitle = @"Malasaña"; 
    ann_80_grados.coordinate = region_80_grados.center; 
    [mapView addAnnotation:ann_80_grados]; 

    //90 Grados 
    MKCoordinateRegion region_90_grados = { {0.0, 0.0}, {0.0, 0.0}}; 
    region_90_grados.center.latitude = 40.4164161; 
    region_90_grados.center.longitude = -3.6699459999999817; 
    region_90_grados.span.longitudeDelta = 0.1f; 
    region_90_grados.span.latitudeDelta = 0.1f; 
    [mapView setRegion:region_90_grados animated:YES]; 

    RestMapPin *ann_90_grados = [[RestMapPin alloc] init]; 
    ann_90_grados.title = @"90 Grados"; 
    ann_90_grados.subtitle = @"Retiro"; 
    ann_90_grados.coordinate = region_90_grados.center; 
    [mapView addAnnotation:ann_90_grados]; 

    /*B&B Babel*/ 
    MKCoordinateRegion region_babel = { {0.0, 0.0}, {0.0, 0.0}}; 
    region_babel.center.latitude = 40.4214535; 
    region_babel.center.longitude = -3.6974301; 
    region_babel.span.longitudeDelta = 0.1f; 
    region_babel.span.latitudeDelta = 0.1f; 
    [mapView setRegion:region_babel animated:YES]; 

    RestMapPin *ann_babel = [[RestMapPin alloc] init]; 
    ann_babel.title = @"B&B Babel"; 
    ann_babel.subtitle = @"Barrio de Chueca"; 
    ann_babel.coordinate = region_babel.center; 
    [mapView addAnnotation:ann_babel]; 

    /*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]; 

    /*Bar Galleta*/ 
    MKCoordinateRegion region_bar_galleta = { {0.0, 0.0}, {0.0, 0.0}}; 
    region_bar_galleta.center.latitude = 40.4227336; 
    region_bar_galleta.center.longitude = -3.7036699999999882; 
    region_bar_galleta.span.longitudeDelta = 0.1f; 
    region_bar_galleta.span.latitudeDelta = 0.1f; 
    [mapView setRegion:region_bar_galleta animated:YES]; 

    RestMapPin *ann_bar_galleta = [[RestMapPin alloc] init]; 
    ann_bar_galleta.title = @"Bar Galleta"; 
    ann_bar_galleta.subtitle = @"Malasaña"; 
    ann_bar_galleta.coordinate = region_bar_galleta.center; 
    [mapView addAnnotation:ann_bar_galleta]; 

    /*Bar Tomate*/ 
    MKCoordinateRegion region_bar_tomate = { {0.0, 0.0}, {0.0, 0.0}}; 
    region_bar_tomate.center.latitude = 40.428041; 
    region_bar_tomate.center.longitude = -3.69047; 
    region_bar_tomate.span.longitudeDelta = 0.1f; 
    region_bar_tomate.span.latitudeDelta = 0.1f; 
    [mapView setRegion:region_bar_tomate animated:YES]; 

    RestMapPin *ann_bar_tomate = [[RestMapPin alloc] init]; 
    ann_bar_tomate.title = @"Bar Tomate"; 
    ann_bar_tomate.subtitle = @"Chamberí"; 
    ann_bar_tomate.coordinate = region_bar_tomate.center; 
    [mapView addAnnotation:ann_bar_tomate]; 




    /*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:@"[email protected]"]; 
    } 
    else { 
     //[mapView.userLocation setTitle:@"I am here"]; 
    } 


    UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    if ([[annotation title] isEqualToString:@"Bacira"]) { 

    } 
    pinView.rightCalloutAccessoryView = pinButton; 


    return pinView; 
} 

-(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 

これは私の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 

最後に、これはレストランの情報が表示されます図である。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 (nonatomic, strong) Restaurant *DetailModal; 

@end 

私は私viewForAnnotation方法を変更する必要がありますが、私はどのように行うのか分からないということkonw。あなたの助けのために非常にあなたより!

答えて

1

右詳細ボタン

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { 
    RestMapPin *annView = view.annotation; 

    YourViewController *objYourVC= [[YourViewController alloc]init]; 
    objYourVC.title = annView.title; 
    objYourVC.subtitle = annView. subtitle; 

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

私の低レベルのobjective-cについては申し訳ありません。私はこのメソッドを書いたが、Xcodeは "宣言されていない識別子 'annView'、 'view'と 'annotation'を使用していると言った。 :(UIControl *)control { annotation * annView = view.annotation; RestViewController * objYourVC = [[RestViewController alloc] init]; objYourVC.TitleLabel = annView。タイトル; objYourVC.DescriptionLabel = annView。字幕; [self.navigationController pushViewController:objYourVC animated:YES]; } ' –

+0

Map Pinのボタンを押したときに開くビューであるRestViewController.hの質問を更新しました。 –

+0

annotation * annView = view.annotationこれを削除してこの行を書く RestMapPin * annView = view.annotation –

0

のための使用、このデリゲートメソッドを他のポスターはマップビューのデリゲートメソッドmapView:viewCalloutAccessoryControlTapped:を使用し、言ったように。

私は注釈のタイトルを使用してレストランをルックアップすることに反対しています。表示文字列を使用してデータにインデックスを付けるべきではありません。つまり、表示文字列を変更したり、別の言語にローカライズしたりすると、ルックアップが機能しなくなります。

注釈にIDフィールドを追加して使用します。

+0

私はこのメソッドを書いていましたが、Xcodeは "宣言されていない識別子 'annView'、 'view'と 'annotation'を使用していました。 –

+0

エラーを生成するコードと、特定の行がエラーを生成する情報と、取得している完全なエラーメッセージを含めるようにコードを編集します。 –

0

はあなたがRestViewControllerのオブジェクトを作成し、あなたがしているのと同じ、そのコントローラの変数にタイトルを渡す必要があり、あなたの選択した注釈のタイトルを取得した後、今、この

- (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 
} 

ようにそれを行う必要がありますユーザーがリストからレストランを選択したときに実行します。

また、彼の答えに記載されている@ダンカンCとして、あなたは文字列を使用してサーバーからデータを取得すべきではない、文字列を変更することができます。

+0

正常に動作しますが、私は問題があります: 'objYourVC.TitleLabel = selectedAnnotation。title;'という行は 'MapViewController.m'(例えば" Bacira "を示しています)でうまくいきます。しかし、私が別のビュー(' RestViewController .m "の場合、' TitleLabel'は何も持っていません(@ "Label"を示しています)。それを解決する方法はありますか、テキストは別のビューに渡されていないと思います。学ぶために、私は整数を使ってdatを得る次のプロジェクトでは)。ご助力ありがとうございます! –

+0

コードをデバッグしてブレークポイントとNSLogを追加する – Rajat

関連する問題