cc_hey
cc_hey

Reputation: 81

Error in if statement while comparing UITextField's text and a string

In my app, I have a view controller which is having a search bar with UItextfield at the top and a UIImageview below that. The image view is initially hidden.

I want this image view to unhide through an if statement. The user will enter keywords into the textfield and when a certain word will match a pre defined string in the.m file, then it must show the image.

I originally had two view controllers but now I added another one (thirdviewcontroller). As I enter a word into the textfield, the simulator will direct me back to the code highlighting in green on this line:

 if ([string1 isEqualToString:string2])  {

     locationMap.hidden = YES;

This is .h file:

@interface ThirdViewController : UIViewController
{
    IBOutlet UITextField *searchLocation;
    IBOutlet UIImageView *locationMap;
}
-(IBAction)SearchGo;
@end

This is the .m file:

-(IBAction)SearchGo{

    NSString *string1 = searchLocation.text;
    NSString *string2= @"sydney";
    if ([string1 isEqualToString:string2]) {
        locationMap.hidden = YES;
    }
}

Upvotes: 0

Views: 228

Answers (3)

Natasha
Natasha

Reputation: 6903

I am guessing, you have attached the IBAction with your textfield,searchLocation and triggered the action specifying "Touch up Inside". This will not work for couple of reasons.

First of all, you need to implement the textFieldShouldReturn: delegate method, so that your controller knows when you press return, it should hand over the control from your text field. Then again, a you have attached your action method to your text filed, as soon as you tap on the textfield, it goes to your method and start comparing but at this point, you have typed nothing in your textfield and it fails to conform to your if condition.

the solution is to either use the have a button and attach the action method to that button. That way, after you have typed the word "sydney", and you hit on the button. It will take whatever in your textfield and compare to that.

Here is the solution-

enter image description here

See the extra button named "Go". Attach your method to it.

This is my .h file-

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>

@property(nonatomic, strong)   IBOutlet UITextField *searchLocation;
@property(nonatomic, strong)   IBOutlet UIImageView *locationMap;

-(IBAction)SearchGo:(id)sender;

@end

And this is the .m file-

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic, strong) NSString *string1;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

#pragma mark- textfield delegate

- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.string1 = textField.text;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(IBAction)SearchGo:(id)sender{
    NSString *string2= @"Sydney";
    if ([self.string1 isEqualToString:string2]) {
        self.locationMap.hidden = NO;
    }
}

@end

Save the string from the textfield after your editing is done through the delegate method. Make sure, you attach the UITextFieldDelegate to your ViewController.

Alternatively, you may want to avoid all this trouble and use the UISearchDisplay controller.

Upvotes: 0

Omer Waqas Khan
Omer Waqas Khan

Reputation: 2413

In viewDidLoad method, use:

locationMap.hidden = YES;

In your -(IBAction)SearchGo method, use:

locationMap.hidden = NO;

OR for your searchGo method:

-(IBAction)SearchGo{

    if ([searchLocation.text isEqualToString:@"sydney"]) {
        locationMap.hidden = NO;
    }else {
       //implementation
    }
}

Upvotes: 0

johnpatrickmorgan
johnpatrickmorgan

Reputation: 2372

It sounds like you've accidentally set up a breakpoint. Simply remove the breakpoint by clicking the blue arrow to the left of the line it breaks on.

Upvotes: 1

Related Questions