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

时间:2015-06-30 16:08:08

标签: ios objective-c mkmapview

编辑:原始声明: 我在一个视图控制器上有一个嵌入了mapView的导航控制器,它通过使用标注将segue推送到第二个视图控制器。用户点击标注上的信息键,然后他们进入第二个视图控制器,他们必须填写表格。一旦他们点击第二个视图控制器导航栏中的后退键,我如何将他们填写的表格的前两行链接到该针的标题和副标题?

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

首先查看controller.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];
}

第二个视图controller.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

第二个视图controller.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;
}

1 个答案:

答案 0 :(得分:0)

You should add a delegate pattern to pass the info from your second view controller back to your first view controller.

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
}