I am writing an iPad app that has a Cancel button on a view controller. When the work on the controller is finished the labelText changes from “Cancel” to “Done”. Therefore I only want the alert to show when the labelTitle is still “Cancel”. If it is “Done” I just want the controller to dismiss. Here is the code:
NSLog(@"%@",closeButton.titleLabel.text);
if (closeButton.titleLabel.text = @"Cancel")
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"i-observe"
message:@"Are you sure that you want to cancel?" delegate:self cancelButtonTitle:@"no" otherButtonTitles:@"yes", nil];
[alert show];
}
else
{
[self dismissViewControllerAnimated:YES completion:nil];
}
What happens is that the alert appears in both cases (“Cancel” and “Done”). However in the NSLog it shows that the titleLabel has changed. Any ideas?
First of all, you are setting the text to “Cancel” within your if statement. So, your if statement logic says “if the title text is successfully set to ‘Cancel’, then display the alert.” The comparison operator is
==, not=.Second, you do not want to compare two different text objects with the
==operator. You want to useisEqualToString:like this: