I'm trying to build a login page for an app. Something went wrong when testing the login condition.
There are two textfields, username and password. When the Login button is clicked, alert shows when
1) either fields are empty
2) both filled.
The code reflects this below. But the result came out no matter that the textfield is empty or not, it only showed as textfields were filled ( only the else part run). So I assume there was something wrong with the if condition. But when I checked the if condition in other view, it worked.
Can anyone help?
if ([_pLoginIDField.text isEqualToString:#""] || [_pPasswordField.text isEqualToString:#""]) {
UIAlertView *error = [[UIAlertView alloc] initWithTitle:#"Yeah !" message:#"must complete all fields" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[error show];
}
else {
UIAlertView *error2 = [[UIAlertView alloc] initWithTitle:#"Oooops !" message:#"hhhhhhhhhh" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[error2 show];
}
Try this:
if (_pLoginIDField.text.length > 0 || _pPasswordField.text.length > 0)
NSString *text = [NSString stringWithString: nameUITextField.text];
if(text.length == 0){
}
You may try this,
you can count how many characters are in string, it is indirectly checking to null.
NSString *inputString = [NSString stringWithString: nameUITextField.text];
if(inputString.charachters.count == 0){
// inputString is Blank
// Do your stuff
}else{
//inputString is not blank, Do other Stuff.
}
the above condition satisfy if inputString is blank otherwise it will enter into else block.
Related
I'm building an iOS app and I need to provide a forgot password function. I have a db that stores registered users and their details(userArray). I have searched by email inputed so, how do I do a UIAlert that shows the found password??? below is code I tried but my if condition is wrong.
[self.theuser valueForKey:#"password"] is the retrieved password from the database and its a string.
- (IBAction)ForgotPassword:(id)sender {
if ([[self.theuser valueForKey:#"password"] > 0 ]) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Success"
message: #"Your Password is:"
delegate: self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
You need to check that the length of the string is higher than 0, because using this:
if ([self.theuser valueForKey:#"password"])
will always return true if the value is other than nil, meaning if you server code returns that the value of password is "" (two quote marks with no space in between) then the app will recognize that the value is not nil and display an alert with no text in it
So the final code should look something like this:
- (IBAction)ForgotPassword:(id)sender {
NSString *retrievedPassword = [self.theuser valueForKey:#"password"];
if ( retrievedPassword.length > 0) {
NSString *message = [NSString stringWithFormat:#"Your password is: %#", retrievedPassword];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Success"
message: message
delegate: self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
}
Change:
#"Your Password is:"
to:
[NSString stringWithFormat:#"Your Password is: %#", [self.theuser valueForKey:#"password"]]
You can also change this:
if ([[self.theuser valueForKey:#"password"] > 0 ]) {
to this:
if ([self.theuser valueForKey:#"password"]) {
If it didn't exist, it would count as "false". Any other value is "true".
Firstly I have this code which saves my video file in the documents directory:
filePath = [NSString stringWithFormat:#"%#/Documents/%f.mp4", NSHomeDirectory(),[[NSDate date] timeIntervalSince1970]];
Secondly I have a UIAlertView that allows the user to enter the name they want for the video they are saving:
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"GIVE YOURE VIDEO A NAME" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
And I have this to display what is entered:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
if (alertView.alertViewStyle == UIAlertViewStylePlainTextInput){
NSLog(#"Entered: %#",[[alertView textFieldAtIndex:0] text]);
}
}
else
{
NSLog(#"user pressed Button Indexed 1");
// Any action can be performed here
}
}
Know that I want to do is to save the video file with a name specified by text which was entered in to the UIAlertView. To do this I need to change this line: filePath = [NSString stringWithFormat:#"%#/Documents/%f.mp4", NSHomeDirectory(),[[NSDate date] timeIntervalSince1970]]; which saves the file as the current date and time to the entered text. However, I do not know how I would go about doing that.
Are you asking how to replace the date with the text entered ?
You only need to pass in the string, which you are already doing with the homeDirectory, like so
filePath = [NSString stringWithFormat:#"%#/Documents/%#.mp4", NSHomeDirectory(),[[alertView textFieldAtIndex:0] text]];
I have a calculator that multiplies a value with a certain $ rate. The user can edit the $ rate, and the value is multiplied with the user-entered rate. Here is my code for this:
- (IBAction)edit
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Edit New Amount"
message:#"Enter new rate"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex) {
UITextField *field = [alertView textFieldAtIndex:0];
field.placeholder = #"Enter New Rate";
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789."] invertedSet];
if ([field.text rangeOfCharacterFromSet:set].location != NSNotFound)
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Only numbers are allowed in this field."delegate:self cancelButtonTitle:#"OK."otherButtonTitles:nil];
[errorAlert show];
rate.text=#"";
}
else
{
rate.text = field.text;
}
}
else
{
//Cancel
}
}
However, when the app closes and is restarted, the old rate is displayed, not the new user-entered rate. How can I save the new user-entered rate, so that it is retained within the app, even when it is restarted?
If you want the value to be retained after app is closed, then you got to store it somewhere in file system. Easiest and convenient way is to use NSUserDefaults:
//Storing in user defaults
[[NSUserDefaults standardUserDefaults] setObject:field.text forKey:RATE_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
//Retrieving (possibly in app start or viewdidload)
self.rate.text = [[NSUserDefaults standardUserDefaults] objectForKey:RATE_KEY];
One note on usage of NSUserDefaults is that it doesn't support storing every object type, it supports NSString,NSArray,NSNumber etc and primitive types. Since you used NSStrings in your code I assumed you can save it as string to defaults.
Have you tried creating a new variable to hold the new input as the user enters the new input, and then setting whatever the new input is to that new variable? It sounds like it is restarting from the last variable that was set in the input.
I want the user to confirm the password he typed it... so I use two text fields.. But somehow even if both have the same password it seems to think that the 2 strin differ
if (![self.typePTextField.text isEqualToString:self.retypePLabel.text]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error", #"Error") message:NSLocalizedString(#"Passwords do not match \n please retype", #"Passwords do not match \n please retype") delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
self.typePTextField.text = #"";
self.retypePLabel.text = #"";
return;
}
The alert appears even if I type the same string twice... and only the first text field geets reset to #"" ...
What will fix this?
I'm guessing in the isEqual: method
self.retypePLabel.text
should be
self.retypePTextField.text
try
if (![self.typePTextField.text isEqualToString:self.sometextfield.text]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error", #"Error") message:NSLocalizedString(#"Passwords do not match \n please retype", #"Passwords do not match \n please retype") delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
self.typePTextField.text = #"";
self.sometextfield.text = #"";
return;
}
if([_txtPassword.text isEqual:_txtconfirmPassword.text])
{
NSLog(#"Password =%# , ConfirmPassword = %# ",_txtPassword.text,_txtconfirmPassword.text);
}
else {
//// code show alert////
}
self.retypePLabel.text? are you sure you ask about this, because I think it should be like self.retypePTextField.text
How do i compare a website result with a predicted result.
#"document.getElementsByTagName('body')[0].outerHTML"
is predicted to contain:
<body>OK</body>
But i always get an error saying that they don't match. I used this code below to compare them:
if (webresult == cmp){
then it shows an alert saying success. Or in else it'll say error. It always goes to else. Heres the code block, Please help.
- (IBAction)displayresult:(id)sender {
webresult = [webview2 stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body')[0].outerHTML"];
NSString *cmp = [[NSString alloc] initWithFormat:#"<body>OK</body>"];
if (webresult == cmp) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Logged in" message:#"Logged in, Proceeding to the game" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:webresult delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
I assume that webresult is an NSString. If that is the case, then you want to use:
if ([webresult isEqualToString:cmp]) {
instead of:
if (webresult == cmp) {
as the above method checks if the strings are equal character by character, whereas the bottom method checks if the two strings are the same pointer.
if (webresult == cmp)
Here, == checks whether webresult, cmp are pointing to the same reference or not. You should instead compare value of the object by using NSString::isEqualToString.
if ( [ cmp isEqualToString:webresult ]) {
// ..
}else {
// ..
}
Note that isEqualToString is a good option because it returns boolean value.
We cannot comapre the strings with ==
We have to use isEqualToString:
if([str1 isEqualToString:str2])
{
}
else
{
}
When comparing strings you need to use isEqualToString:
if ([cmp isEqualToString:webresult]) {
...
} else {
...
}
for Swift 4.0:
if str1==str2 {
//both string are equal
}
else {
//do something expression not true
}
for Objective-C:
if ([str1 isEqualToString:str2]) {
//both string are equal
}
else {
//do something expression not true
}