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.Mo Abdul-Hameed– Mo Abdul-Hameed2017-06-06 12:31:50 +00:00Commented Jun 6, 2017 at 12:31
-
Are you sure that your text field's delegate is set properly?Mo Abdul-Hameed– Mo Abdul-Hameed2017-06-06 12:32:16 +00:00Commented Jun 6, 2017 at 12:32
-
Possible duplicate of swift - validating UITextFieldAdrian– Adrian2017-06-06 12:33:21 +00:00Commented Jun 6, 2017 at 12:33
-
1Don'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– Duncan C2017-06-06 12:33:27 +00:00Commented Jun 6, 2017 at 12:33
-
your if...else condition is wrong. in if statement sbutton.enable = yesBirendra– Birendra2017-06-06 12:39:30 +00:00Commented Jun 6, 2017 at 12:39
Add a comment
|
3 Answers
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;
}
}
Comments
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
Kavitha Madhu
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.
Duncan C
Edit your question to show your updated code. (not a screen-shot of your code, but your actual code as text.)
-(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;
}