我有六种方法,我可以调用警报功能 是否可以只调用一次警报功能,所以优化我的代码将是什么? 这是我的代码:
// #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._%+-]+@[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 :(得分:0)
编写此方法并调用您想要的地方。
-(void)showAlert :(NSString *)alertTitleString WithAlertMessage :(NSString *)alertMessage{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:alertTitleString alertMessage delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
答案 1 :(得分:0)
当然你可以,你甚至应该!你只需要将它重构为一个单独的方法,它将获得一些属性或(更好的选项)一个带有变量的对象,你可以从中为你想要显示的警报设置所需的值。
- (void)showAlertWithTitle:(NSString *)alertTitle andMessage:(NSString *)alertMessage {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertTitle message:alertMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
这可能比一种更好的方式,但根据你的情况,你会做你想要的。 它只是从您的代码中提取方法。
答案 2 :(得分:0)
可以在AppDelegate.h中添加此方法名称
-(void)alertFuction:(NSString *)title message:(NSString *)message
然后将此功能添加到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];
}
然后在任何其他视图控制器中放置此项 .h文件
@class AppDelegate
.m文件
#import AppDelegate
在viewDidload方法
中AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
然后您可以通过
调用警报方法[delegate alertFuction:@"title" message:@"message"];
答案 3 :(得分: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
请注意,只有在需要时才会返回警报视图(假设您需要在操作完成后自动删除警报视图)。