2011-08-30 4 views
0

I이 경고는 결과에 따라 표시 다음 코드 : 나는 다음과 같은 코드를 향상시킬 수있는 방법 :Objective-C에서 다음 코드를 향상시키는 방법은 무엇입니까?

-(IBAction)saveSettings:(id)sender 

{ UIAlertView를 * 경고 = 무기 호;

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]; 

}  

}

은 내가 경고를 너무 여러 번 인스턴스화하고 반복의 종류를 보인다라고 생각!

답변

3
username = self.usernameTextField.text; 
token = self.passwordTextField.text; 

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" 
               message:@"" 
               delegate:self 
             cancelButtonTitle:nil 
             otherButtonTitles:@"Ok", nil]; 
[alert autorelease]; 

// validate the username and token 
if(![self isValid]) 
{ 
    // show alert that it is not valid 

    alert.message = @"Invalid User Name or Password"; 
    [alert show]; 
    return; 
} 

BOOL isSynched = [self syncSettings]; 

if(!isSynched) 
{ 
    alert.message = @"Error Syncing Settings"; 
} 
else 
{ 
    alert.title = @""; 
    alert.message = @"Settings has been Synced Syncing"; 

}  
[alert show]; 
1

... 최종 넣어에서 다음 &에서 [alert show];의 모든 인스턴스를 떠나보십시오

if (!alert) return; 
[alert show]; 
는 경고 속성의 무리를 설정하고 있기 때문에

phix23의 대답도 좋은 향상을 제공 명시 적으로 & 한 번만. 당신은 또한 ...로 조건을 변경하여

if (alert.message != @"") return; 

건배, 페드로 :

을 내 사용할 수있는 사용하는 경우
관련 문제