2017-01-26 2 views
0

私は、ユーザーが自分のスポットを追加できるtextFieldを持つマップキットを持っています。プログラムは動作しますが、何か入力してEnterキーを押すと何も起こりません。新しいピンは設定されません。誰かが間違っていることを教えてもらえますか?ピンクラスのコードMapKitアノテーションの問題:マップ上に新しいスポットを作成できません

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 

@interface Pin : NSObject <MKAnnotation> 

@property (nonatomic, strong) NSString *name; 
@property (nonatomic, strong) NSDate *createdAt; 
@property (nonatomic) CLLocationCoordinate2D coordinate; 

- (instancetype)initWithName:(NSString*)name andCoord:(CLLocationCoordinate2D)coord; 



+ (instancetype)spotWithName:(NSString*)name andCoord:(CLLocationCoordinate2D)coord; 

- (NSString*)title; 

@end 

Pin.m:

#import "Pin.h" 

@implementation Pin 

- (instancetype)initWithName:(NSString *)name andCoord:(CLLocationCoordinate2D)coord { 
    if (self = [super init]) { 
     _name = name; 
     _coordinate = coord; 
     _createdAt = [NSDate date]; 
    } 
    return self; 
} 

+ (instancetype)spotWithName:(NSString *)name andCoord:(CLLocationCoordinate2D)coord { 
    return [[Pin alloc] initWithName:name andCoord:coord]; 
} 

- (NSString *)title { 
    return _name; 
} 

- (NSString *)subtitle { 
    return [_createdAt description]; 
} 

- (NSString *)description { 
    return [NSString stringWithFormat:@"%@ [%@]", _name, _createdAt]; 
} 

ビューコントローラ実装:

#import "ViewController.h" 
#import <MapKit/MapKit.h> 
#import "Pin.h" 


@interface ViewController()<MKMapViewDelegate, CLLocationManagerDelegate,UITextFieldDelegate> 

@property (weak, nonatomic) IBOutlet MKMapView *mapView; 

@property (weak, nonatomic) IBOutlet UILabel *latitude; 

@property (weak, nonatomic) IBOutlet UITextField *nameInput; 



@property (strong, nonatomic) CLLocationManager *locationManager; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    _mapView.showsUserLocation=YES; 
    _mapView.showsBuildings=YES; 
    _locationManager = [[CLLocationManager alloc] init]; 

    [_locationManager requestAlwaysAuthorization]; 
    [_locationManager startUpdatingLocation]; 

    _mapView.delegate = self; 
    _locationManager.delegate=self; 
} 


- (IBAction)userLoc:(UIButton *)sender { 
    MKCoordinateRegion region = MKCoordinateRegionMake(_mapView.userLocation.coordinate, MKCoordinateSpanMake(37.78583400, 122.40641700)); 

    [_mapView setRegion:region animated:NO]; 
} 




#pragma mark - CCLocationManagerDelegate methods 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations { 

    CLLocation *loc = [locations objectAtIndex:0]; 

    _latitude.text = [NSString stringWithFormat:@"%g º", loc.coordinate.latitude]; 
} 
#pragma mark - MKMapViewDelegate methods 

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { 
    _mapView.centerCoordinate = userLocation.location.coordinate; 
} 
#pragma mark - UITextFieldDelegate methods 
- (BOOL)textFieldShouldReturn:(UITextField *)nameInput { 
    Pin *pin = [Pin spotWithName:nameInput.text andCoord:_mapView.userLocation.coordinate]; 

    [_mapView addAnnotation:pin]; 

    nameInput.text = @""; 
    [nameInput resignFirstResponder]; 

    return NO; 
} 

@end 
+0

「何か入力してEnterキーを押したときに...」と言っても、それから説明しないでください。私はあなたのアプリケーションにテキストフィールドを持っていると、ビューコントローラは、それがdelegateだと、textFieldShouldReturnのコードは、トリガする必要がありますし、注釈をマップに追加するコードですか?あなたのコードにメソッドが呼び出されていることを確認するNSLogステートメントを追加しようとしましたか?あなたは何をやっているのかを見て、あなたのコードを踏んだことがありますか? –

+0

はい、その方法は、マップにピンポイントを追加する必要がありますが、シミュレータで、単語を入力してEnterキーを押すと、ピンはそれに訴えられたように現れません...エラーは見つかりません。すべてが大丈夫と思われる... – FuManchu

+0

よく、私はメソッドが呼び出されていないと思う、私は他のようなログを参照してください...しかし、なぜ呼び出されていない? – FuManchu

答えて

0

基本的には、1行が欠落していました:

_nameInput.delegate=self; 

私が持っている場合実装上のUITextfieldプロトコルこの場合、mapKitやCCLocation Managerのように、代理人を割り当てる必要があります

+0

iOS開発のルール1:あなたのコードは、アウトレット、IBAction、代理/データソースの接続を確認してください。ルール2:ルール1を繰り返します。ルール3:ルール2を参照してください。 –

関連する問題