D
D
degterev2013-12-24 13:52:07
Objective-C
degterev, 2013-12-24 13:52:07

Why is the textFieldShouldReturn method not being called?

Hello, I recently started learning iOS programming. I don’t understand what the problem is, the textFieldShouldReturn method is not called
Here is the program code:
ViewController.m

#import "ViewController.h"
#import "BNRMapPoint.h"

@implementation ViewController

-(void)viewDidLoad
{
        locationManager = [[CLLocationManager alloc] init];
        [locationManager setDelegate:self];
    
        [locationManager setDistanceFilter:50];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//    if ([CLLocationManager headingAvailable] == NO) {
//
//        NSLog(@"No Compass. This device does not have the ability to measure magnetic fields.");
//    }else{
        
//        locationManager.headingFilter = 5;
        
//        [locationManager startUpdatingHeading];
//}
    [worldView setDelegate:self];
    [worldView setShowsUserLocation:YES];
    
}

-(void)findLocation
{
    [locationManager startUpdatingLocation];
    [activityIndicator startAnimating];
    [locationTitleField  setHidden:YES];
}

-(void)foundLocation:(CLLocation *)loc
{
    CLLocationCoordinate2D coord = [loc coordinate];
    BNRMapPoint *mp = [[BNRMapPoint alloc] initWithCoordianate:coord title:[locationTitleField text]];
    
    [worldView addAnnotation:mp];
    
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
    [worldView setRegion:region animated:YES];
    
    [locationTitleField setText:@""];
    [activityIndicator stopAnimating];
    [locationTitleField setHidden:NO];
    [locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"%@", newLocation);
    
    NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
    
    if (t < -180) {
        return;
    }
    [self foundLocation:newLocation];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    NSLog(@"Could not find location: %@", error);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    NSLog(@"%@", newHeading);
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldView setRegion:region animated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self findLocation];
    
    [textField resignFirstResponder];
    
    return YES;
    
}



@end

ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate>
{
    
    CLLocationManager *locationManager;
    
    IBOutlet MKMapView *worldView;
    IBOutlet UIActivityIndicatorView *activityIndicator;
    IBOutlet UITextField *locationTitleField;
}
-(void)findLocation;
-(void)foundLocation:(CLLocation *)loc;
@end

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Alexey Storozhev, 2013-12-24
@degterev

Does locationTitleField have its delegate set to File's owner?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question