0

here I try this code.......enter image description here I try this code...I want to disable a button while validating a username and password. if my username and password is correct then my button will enable...But I didn't get correct answer.

5
  • Add your code as text not picture please. Commented Jun 6, 2017 at 12:31
  • Are you sure that your text field's delegate is set properly? Commented Jun 6, 2017 at 12:32
  • Possible duplicate of swift - validating UITextField Commented Jun 6, 2017 at 12:33
  • 1
    Don't post an image of your code. Copy the code into your question, select it, and use the code formatter to format it as code. What is the problem you're having? Are you having a compiler error? A runtime error? Is your code running without error but not disabling the button as you expect it to? Commented Jun 6, 2017 at 12:33
  • your if...else condition is wrong. in if statement sbutton.enable = yes Commented Jun 6, 2017 at 12:39

3 Answers 3

1

If you don't set delegate to your text field then set the delegate as follows

self.stext1.delegate = self;
self.stext2.delegate = self;

Then implement this function

-(void)enableDisableSave {

    NSString * userName = @"kavitha";
    NSString * password = @"kavitha";

    if([userName isEqualToString:self.stext1.text] && [password isEqualToString:self.stext2.text]) {
        self.sendBtn.enabled = YES;
    } else {
        self.sendBtn.enabled = NO;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

It looks to me like sbutton is an IBAction, not an outlet. If so your code probably won't compile. You need to control-drag from your button into your code to create an IBOutlet for your button and use the outlet to enable/disable the button.

2 Comments

Thank u sir.. I create a outlet and done what you said..But my button is still disable after entering a correct username and password.
Edit your question to show your updated code. (not a screen-shot of your code, but your actual code as text.)
0
-(void)checkValidation
{
    NSString *strUserName = [_txtUsername.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *strPassword = [_txtPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    if([strUserName isEqualToString:@"YOURSTRING"] && [strPassword isEqualToString:@"YOURSTRING"])
    {
        // make your button enable
    }
    else
    {
        // make it disable
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    newString = [newString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    [self performSelector:@selector(checkValidation) withObject:nil afterDelay:0.3];

    return TRUE;
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.