2012-10-17 29 views
0

我是iOS編程新手,在視圖控制器之間傳遞數據時遇到問題。如何將註釋標題傳遞給另一個視圖控制器

我有一個視圖控制器,在地圖視圖上對地圖註釋進行地理編碼,並將地址設置爲註釋視圖的標題。註解視圖具有一個標註按鈕,將另一個視圖推送到具有標籤的堆棧,並且我希望將經過地理編碼的地址顯示爲標籤。下面是一些代碼:

這是我把針:

-(void)press:(UILongPressGestureRecognizer *)recognizer 
{ 
    CGPoint touchPoint = [recognizer locationInView:worldView]; 
    CLLocationCoordinate2D touchMapCoordinate = [worldView convertPoint:touchPoint toCoordinateFromView:worldView]; 

    geocoder = [[CLGeocoder alloc]init]; 
    CLLocation *location = [[CLLocation alloc]initWithCoordinate:touchMapCoordinate 
                altitude:CLLocationDistanceMax 
              horizontalAccuracy:kCLLocationAccuracyBest 
              verticalAccuracy:kCLLocationAccuracyBest 
                timestamp:[NSDate date]]; 
    [geocoder reverseGeocodeLocation:location 
        completionHandler:^(NSArray *placemarks, NSError *error) { 
         NSLog(@"reverseGeocoder:completionHandler: called"); 
         if (error) { 
          NSLog(@"Geocoder failed with error: %@", error); 
         } 
         if (placemarks && placemarks.count > 0) 
         { 
          CLPlacemark *place = [placemarks objectAtIndex:0]; 
          _address = [NSString stringWithFormat:@"%@ %@, %@ %@", [place subThoroughfare], [place thoroughfare], [place locality], [place administrativeArea]]; 

          if (UIGestureRecognizerStateBegan == [recognizer state]) { 
           _addressPin = [[MapPoint alloc]initWithCoordinate:touchMapCoordinate 
                     title:_address]; 
          [worldView addAnnotation:_addressPin]; 
         } 
        } 
       }]; 
} 

這裏是我想要的地址顯示視圖代碼:

#import <UIKit/UIKit.h> 
@class MapPoint; 

@interface PinViewController : UIViewController <UINavigationControllerDelegate,  UIImagePickerControllerDelegate,UITextFieldDelegate> 

{ 
    __weak IBOutlet UILabel *addressLabel; 
} 

@property (weak, nonatomic) IBOutlet UIImageView *imageView; 
@property (strong, nonatomic) MapPoint *pin; 

-(void)takePicture:(id)sender; 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 

@end 

#import "PinViewController.h" 
#import "MapPoint.h" 

@interface PinViewController() 

@end 

@implementation PinViewController 

@synthesize imageView, pin; 
-(void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    [addressLabel setText:[pin title]]; 

} 

類的MapPoint有一個標題屬性和字幕屬性。當PinViewController被推到堆棧頂部時,我嘗試將標籤的文本設置爲該標籤的標題,但標籤不顯示任何文本。有人能幫助我,並告訴我我失蹤了嗎?非常感謝您的幫助。

+1

你不顯示在您創建銷視圖控制器代碼,設置其引腳並推到導航堆棧。 – jrturton

+0

當你想在標籤上顯示標題...點擊插針或揭示按鈕後...? – Rajneesh071

+0

我想在PinViewController的標籤上按下披露按鈕後顯示標題。 –

回答

1

的標籤只需創建屬性,並傳遞來自標註挖掘方法的標籤文本

- (void)mapView:(MKMapView *)mapView annotationView:(MKPinAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { 

    Details *det=[[Details alloc]init]; 
    det.label.text=annotation.title; 
    [self.navigationController pushViewController:det animated:YES]; 
    [det release]; 
} 
+0

我實現了這種方法,但當我按下標註按鈕時,我的應用程序崩潰。我得到錯誤exc_bad_access code = 1 –

+0

當我實現一個自定義方法時,應用程序不會崩潰,但它不起作用。這裏是方法:' - (void)showDetails:(id)sender { NSLog(@「點擊附件按鈕進行註釋」); PinViewController * pinViewController = [[PinViewController alloc] init]; pinViewController.label.text = _addressPin.title; [[self navigationController] pushViewController:pinViewController animated:YES]; }' –

2

你必須爲這個問題分享更多的代碼塊:)。而不是你必須得到更多的想法在面向對象的程序有關通信

你可以閱讀有關傳遞數據視圖類之間here

讀最好的方式一個很好的教程通過兩種觀點here

之間的對象

這是一個很好的文件,我在Developer.apple找到有關CommunicatingWithObjects

你還可以觀看一個好的視頻here

相關問題