将标签或文本字段设置为注释标题



编辑:原始语句:我有一个导航控制器,在一个视图控制器上嵌入了mapView,它通过使用callout将segues推入第二个视图控制器。用户点击标注上的信息键,然后进入第二个视图控制器,在那里他们必须填写表格。一旦他们点击第二个查看控制器导航栏中的返回键,我将如何将他们填写的表格的前两行链接为pin的标题和副标题?

更新:我疯了,因为我在谷歌上搜索并重新制作了我的XCode项目来实现这一目标。我不喜欢让任务未完成。当我返回第一个视图控制器时,我的字符串和文本字段为零。

第一视图控制器.h文件

#import <UIKit/UIKit.h>
#import "mapKit/Mapkit.h"
#import "thePinsViewController.h"
@interface ViewController : UIViewController <MKMapViewDelegate, MKAnnotation, thePinsViewController> {
    MKMapView *mapViewing;
    MKPointAnnotation *annot;
}
@property(nonatomic, retain) IBOutlet MKMapView *mapViewing;
@end

第一视图controller.m文件

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize coordinate = _coordinate;
// This is your IBOutlet
@synthesize mapViewing;
// Initiate when the user holds the map to place a pin
- (void) addGestureRecognizerToMapView {
    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handleLongPress:)];
    // User needs to press for [insert number in decimal format] seconds
    lpgr.minimumPressDuration = 1.0;
    [mapViewing addGestureRecognizer:lpgr];
}
// This method fires when you add a pin
- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateBegan) {
        return;
    }
    CGPoint touchPoint = [gestureRecognizer locationInView: mapViewing];
    CLLocationCoordinate2D touchMapCoordinate = [mapViewing convertPoint:touchPoint toCoordinateFromView:mapViewing];
    annot = [[MKPointAnnotation alloc] init];
    annot.title = @"a";
    annot.subtitle = @"SubTitle";
    annot.coordinate = touchMapCoordinate;
    [mapViewing addAnnotation:annot];
}
// This is your manual callout box
// with a rightDisclosureButton embedded in it
- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""];
    // This gives the user permission to see or not see the callout box
    annotationView.canShowCallout = YES;
    // This is the info button in the callout box
    UIButton *rightDisclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annotationView.rightCalloutAccessoryView = rightDisclosureButton;
    return annotationView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    thePinsViewController *scoobydoo = [[thePinsViewController alloc] init];
    [self performSegueWithIdentifier:@"heylisten" sender:view];
    scoobydoo.delegate = self;
}
- (void) didFirstFieldChange:(NSString*)newValue{
    // Change your annotation title here
    annot.title = newValue;
}
- (void) didSecondFieldChange:(NSString*)newValue{
    // Change your annotation title here
    annot.subtitle = newValue;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [mapViewing setDelegate:self];
    [self addGestureRecognizerToMapView];
}

第二视图控制器.h文件

#import <UIKit/UIKit.h>
@protocol thePinsViewController <NSObject>
- (void) didFirstFieldChange:(NSString*)newValue;
- (void) didSecondFieldChange:(NSString*)newValue;
@end
@interface thePinsViewController : UIViewController
@property (nonatomic, weak) id<thePinsViewController> delegate;
@property(nonatomic, strong) IBOutlet UITextField *helloWorldTextField;
@property(nonatomic, strong) IBOutlet UITextField *helloWorldTextField2;
@property(nonatomic, strong) NSString *helloWorldString;
@end

第二视图控制器.m文件

#import "thePinsViewController.h"
@interface thePinsViewController ()
@end
@implementation thePinsViewController
@synthesize helloWorldTextField;
@synthesize helloWorldTextField2;
@synthesize helloWorldString;
- (void) firstFieldChanged{
    if ( [self.delegate performSelector:@selector(didFirstFieldChange:)] ){
        [self.delegate didFirstFieldChange:helloWorldTextField.text];
    }
}
- (void) secondFieldChanged{
    if ( [self.delegate performSelector:@selector(didSecondFieldChange:)] ){
        [self.delegate didSecondFieldChange:helloWorldTextField2.text];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    helloWorldString = helloWorldTextField.text;
}

您应该添加一个委托模式,将信息从第二个视图控制器传递回第一个视图控制器。

PinInformationViewController.h

@protocol PinInformationViewControllerProtocol <NSObject>
- (void) didFirstFieldChange:(NSString*)newValue;
- (void) didSecondFieldChange:(NSString*)newValue;
@end
@interface PinInformationViewController : UIViewController
@property (nonatomic, weak) id<PinInformationViewControllerProtocol> delegate;
@end

PinInformationViewController.m

- (void) firstFieldChanged{
    if ( [self.delegate performSelector:@selector(didFirstFieldChange:)] ){
        [self.delegate didFirstFieldChange:myTextField.text];
    }
}
- (void) secondFieldChanged{
    if ( [self.delegate performSelector:@selector(didSecondFieldChange:)] ){
        [self.delegate didSecondFieldChange:myOtherTextField.text];
    }
}

FirstViewController.m

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    pinInformationViewController *scoobydoo = [[pinInformationViewController alloc]init];
    scoobydoo.delegate = self;
    annot.title = scoobydoo.helloWorldString;
    [self performSegueWithIdentifier:@"heylisten" sender:view];
}
- (void) didFirstFieldChange:(NSString*)newValue{
    // Change your annotation title here
}
- (void) didSecondFieldChange:(NSString*)newValue{
    // Change your annotation subtitle here
}