I have the following code that displays the alerts depending on the result: How can I improve the following code:
-(IBAction)saveSettings:(id)sender
{
UIAlertView *alert = nil;
username = self.usernameTextField.text;
token = self.passwordTextField.text;
// validate the username and token
if(![self isValid])
{
// show alert that it is not valid
alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Invalid User Name or Password" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
[alert show];
return;
}
BOOL isSynched = [self syncSettings];
if(!isSynched)
{
alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error Syncing Settings" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
[alert show];
}
else
{
alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Settings has been Synced Syncing" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
[alert show];
}
}
I think I am instantiating the alert too many times and looks kind of repetitive!
1 Answer