如何将注释标题传递给另一个视图控制器

时间:2012-10-17 06:01:25

标签: iphone ios objective-c ipad

我是iOS编程的新手,我在视图控制器之间传递数据时遇到了麻烦。

我有一个视图控制器,它在地图视图上对注释进行地理编码,并将地址设置为注释视图的标题。注释视图有一个callout按钮,可以将另一个视图推送到具有标签的堆栈,我希望地理编码的地址显示为标签。这是一些代码:

这是我放下引脚的地方:

-(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类具有title属性和subtitle属性。当PinViewController被推到堆栈的顶部时,我尝试将标签的文本设置为引脚的标题,但标签不显示任何文本。有人能帮帮我,告诉我我错过了什么吗?非常感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您必须为此问题分享更多代码块:)。而不是你必须更多地了解面向对象程序中的通信

您可以阅读有关在视图类之间传递数据的良好教程here

阅读在两个视图之间传递对象的最佳方法here

我在Developer.apple中找到了一份关于CommunicatingWithObjects

的好文档

您还可以观看精彩视频here

答案 1 :(得分:1)

只需为标签创建属性,并从callout tapped方法

传递标签文本
- (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];
}