Talha Ch Talha Ch - 12 days ago 6
iOS Question

NSString Comparison is not working on iPAD

I'm getting this strange and simple issue when I try to compare two NSString in

if
condition. It works perfectly in case of iPhone but on iPad it didn't work.

I have four
UITextFields
and all have
delegate = self
. Upon
textFieldShouldBeginEditing
I'm returning
NO
and there is a call to the method as:

if (textField==self.dx1Field)
{
[self.view endEditing:YES];
[self showDXViewForField:@"1"];
return NO;
}
else if (textField==self.dx2Field)
{
[self.view endEditing:YES];
[self showDXViewForField:@"2"];
return NO;
}
else if (textField==self.dx3Field)
{
[self.view endEditing:YES];
[self showDXViewForField:@"3"];
return NO;
}
else if (textField==self.dx4Field)
{
[self.view endEditing:YES];
[self showDXViewForField:@"4"];
return NO;
}
else return YES;


The method
showDXViewForField
opens up another
ViewController
with
tableView
that shows some values from which user will select one and it is working perfectly fine.
I have made a delegate method which get the selected DX value and here I've applied the checks that if any of the four text fields have that value don't add it and show that it is already added.

-(void)getSelectedDX:(NSString*)dxName forField:(NSString *)fieldNumber
{

if ([fieldNumber isEqualToString:@"1"]) {

if (self.dx2Field.text!=dxName&&self.dx3Field.text!=dxName&&self.dx4Field.text!=dxName) {
self.dx1Field.text = dxName;
}
else
{
[KSToastView ks_showToast:@"This DX is already added!" duration:2.0f];
}

}
else if ([fieldNumber isEqualToString:@"2"])
{
NSLog(@"%@==%@",self.dx1Field.text,dxName);
if (self.dx1Field.text!=dxName&&self.dx3Field.text!=dxName&&self.dx4Field.text!=dxName) {
self.dx2Field.text = dxName;
}
else
{
[KSToastView ks_showToast:@"This DX is already added!" duration:2.0f];
}
} //and same for 3 and 4


The issue is that when I run it on iPAD is didn't work. It sets the duplicate value for text view but the same code runs on iPhone.

Answer

You are comparing pointers, not values the pointers represent. All of your self.dx1Field.text!=dxName style comparisons should be using isEqualToString.

Comments