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.

Kavitha Madhu
  • 396
  • 1
  • 5
  • 19
  • Add your code as text not picture please. – Mo Abdul-Hameed Jun 06 '17 at 12:31
  • Are you sure that your text field's delegate is set properly? – Mo Abdul-Hameed Jun 06 '17 at 12:32
  • Possible duplicate of [swift - validating UITextField](https://stackoverflow.com/questions/31495188/swift-validating-uitextfield) – Adrian Jun 06 '17 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? – Duncan C Jun 06 '17 at 12:33
  • your if...else condition is wrong. in if statement sbutton.enable = yes – Birendra Jun 06 '17 at 12:39

3 Answers3

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;
    }
}
Samiul Islam Sami
  • 791
  • 13
  • 24
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.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • 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. – Kavitha Madhu Jun 06 '17 at 12:57
  • Edit your question to show your updated code. (not a screen-shot of your code, but your actual code as text.) – Duncan C Jun 06 '17 at 15:03
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;
    }
Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56