user991340
user991340

Reputation: 11

How Do I validate UITextfield with Correct Answer String?

I have a UITexfield in Xcode that the user inputs their answer to a question. If the answer is correct they are taken to another screen. All the help I have found only shows how to verify the amount of characters or numbers in a string.

I have declared

NSString *answer = @"The Correct Answer";

and

if ([UITextField1.text isEqualToString:@"The Correct Answer"])

then ...

This is where I get lost (sorry total newbie) I have been able to compare these in NSlog in foundation.h file but get really lost when trying to deal with .h and .m files in xcode.

Can anyone please check if with the above I am on the right track and please explain how I get to display the new screen when the string/answer is correct.

Thanks in Advance

Upvotes: 1

Views: 217

Answers (2)

RamaKrishna Chunduri
RamaKrishna Chunduri

Reputation: 1130

Well I'm not clear on your question.

If you are asking how to show a screen once a user typed the right answer in textbox then use:

  1. Set textfield's delegate to your view controller then handle this method in your view controller:

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    
  2. Just do the needful there, something like:

    if ([textField.text isEqualToString:@"The Correct Answer"])
    {
     [self presentModalViewController:correctVC animated:NO];
     return NO;
    }
    return YES;
    

Upvotes: 0

Totumus Maximus
Totumus Maximus

Reputation: 7573

The check you are making is correct. It checks the precise text of the TextField1 with the precise text of @"The Correct Answer". If it matches you can make your navigationController push a new view.

ea. Like this:

MyViewController *myVC = [[MyViewController alloc] init];
//do something with your new ViewController.view
[self.navigationController pushViewController:myVC animated:YES];

Of course you will have to adjust the new View to your own needs. Also make sure you have access to that navigationController.

But yeah, this is all very basic and you should be able to figure it out for yourself with some proper tutorials.

Upvotes: 1

Related Questions