2014-02-06 33 views
0

我有六種方法,其中我打電話警報功能。
是否有可能只調用一次警報函數,所以優化我的代碼將是?
這是我的代碼:我有7種方法,我打電話警報功能,是否有可能我只打一次性警報功能,所以優化我的代碼

// #import "ViewController.h" 

//.h file 

.m file 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize usernameText; 
@synthesize emailText; 
@synthesize passwordText; 
@synthesize `repasswordTex`t; 
@synthesize postalText; 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

//for keyboard hide on textfield return 
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { 
    [theTextField resignFirstResponder]; 
    return YES; 
} 

//function for Name validation 
- (BOOL) validatename:(NSString *) candidate{ 
    [postalText setKeyboardType:UIKeyboardTypeAlphabet]; 
    NSString *nameRegex = @"[A-Za-z]+"; 
    NSPredicate *codeTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nameRegex]; 

    return [codeTest evaluateWithObject:candidate]; 
} 

// email validation 
- (BOOL) validateEmail: (NSString *) candidate { 
    NSString *emailRegex = @"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 

    return [emailTest evaluateWithObject:candidate]; 
} 

// password code validation 
- (BOOL)paswordvalidation:(NSString *) candidate 
{ 

} 

// postal code validation 
- (BOOL) validatecode: (NSString *) candidate { 
    NSString *codeRegex = @"^\d{5}(?:[-\s]\d{4})?$"; 
    NSPredicate *codeTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", codeRegex]; 

    return [codeTest evaluateWithObject:candidate]; 
} 



//method call on submit button 
    - (IBAction)submitButn:(id)sender{ 



    //user name method call 
      if(![self validatename:usernameText.text]) 
      { 
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please Ensure that you have insert character only"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
       [alert show]; 
       return; 
      } 
//Email Method Call 
    else if(![self validateEmail:emailText.text]) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please Insert Valid Email Address"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     return; 
    } 
//Password validate Method Call 

    else if([passwordText.text length] <6)//&& [repasswordText.text length] <= 6) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Password should contain minimun 6 "delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     return; 
    } 
    else if (![passwordText.text isEqualToString:repasswordText.text]) 
{ 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"confirm password should be same"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     return; 
    } 



    //Postal Code Method Call 
      else if(!([self validatecode:postalText.text] ||[postalText.text length] <6)) 

       { 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"postal Code should cotain 5 "delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
        [alert show]; 
        return; 
       } 
    } 

- (void)didReceiveMemoryWarning 

    { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 

@end

回答

0

寫這個方法,並呼籲在任何你想要的。

-(void)showAlert :(NSString *)alertTitleString WithAlertMessage :(NSString *)alertMessage{ 
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:alertTitleString alertMessage delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
[alert show]; 

} 
+0

我知道你把它重構了它的代碼,但是那個return語句似乎是不必要的 –

+0

這不會以你發佈的方式工作 –

0

當然,你可以,你甚至應該!你只需要將它重構爲一個單獨的方法,女巫會收到一些屬性或(更好的選擇)一個變量的對象,你可以爲它設置一個你想要顯示的警報的必需值。

- (void)showAlertWithTitle:(NSString *)alertTitle andMessage:(NSString *)alertMessage { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertTitle message:alertMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
} 

它可能比一種更好的方式,但根據你的情況,將做你想要的。 它只是從你的代碼中提取一個方法。

+0

'self'不一定想成爲委託。本地化呢?我們可以做得比這更好嗎? – Wain

+0

我們當然可以,但我認爲這很明顯,取決於他的需要。我可以添加更多的參數(確保委託)。隨意提出編輯:) –

0

這是可能的,在AppDelegate.h

添加此方法的名稱
-(void)alertFuction:(NSString *)title message:(NSString *)message 

然後,在把這個 .H任何其他視圖控制器此函數添加到AppDelegate.m

-(void)alertFuction:(NSString *)title message:(NSString *)message 
{ 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil, nil]; 
     [alert show]; 
     [alert release]; 
     alert = nil; 

     [delegate.window setUserInteractionEnabled:YES]; 

} 

然後文件

@class AppDelegate 

.m文件

#import AppDelegate 
在viewDidLoad方法

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; 

,那麼你可以調用

[delegate alertFuction:@"title" message:@"message"]; 
+2

不要使用應用程序委託。應用程序委託用於「應用程序級事件處理委派」 - 不是任意的輔助函數... – Wain

0

的警報系統如何與類方法,涵蓋了一些常見場景的通用助手類(其您可能顯然想重新配置/添加到):

@implementation AlertHelper 

+ (UIAlertView *)showAlertWithMessage:(NSString *)message 
{ 
    [AlertHelper showAlertWithTitle:nil message:message delegate:nil cancelButton:NSLocalizedString(@"OK", nil) otherButton:nil]; 
} 

+ (UIAlertView *)showAlertWithTitle:(NSString *)title message:(NSString *)message 
{ 
    [AlertHelper showAlertWithTitle:title message:message delegate:nil cancelButton:NSLocalizedString(@"OK", nil) otherButton:nil]; 
} 

+ (UIAlertView *)showAlertWithTitle:(NSString *)title message:(NSString *)message delegate:(id <UIAlertViewDelegate>)delegate cancelButton:(NSString *)cancel otherButton:(NSString *)other 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancel otherButtonTitles:(other ? @[ other ] : nil)]; 
    [alert show]; 
} 

@end 

請注意,警報視圖會在需要時返回(例如,您需要在操作完成後自動刪除警報視圖)。

相關問題