这是我学习XCode的第二天,所以我在这里完全是新手,我有这个.m代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[_fieldEmail setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14]];
[_fieldPassword setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14]];
[_titleLogin setFont:[UIFont fontWithName:@"Raleway-ExtraLight" size:28]];
UIView *fieldEmail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldEmail.leftViewMode = UITextFieldViewModeAlways;
_fieldEmail.leftView = fieldEmail;
UIView *fieldPassword = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldPassword.leftViewMode = UITextFieldViewModeAlways;
_fieldPassword.leftView = fieldPassword;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)validateEmail:(NSString *)emailStr {
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:emailStr];
}
- (IBAction)buttonRegister {
_labelOutput.text = @"Register?";
}
- (IBAction)buttonLogin {
if ([_fieldEmail.text length] > 0) {
if(![self validateEmail:[_fieldEmail text]]){
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Email Error"
message:@"That's not a valid email!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}else{
_labelOutput.text = @"Login?";
}
}else{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Login Error"
message:@"Please enter your email"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
}
- (IBAction)loginFacebook {
if ([_fieldPassword.text length] > 15) {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Password Error"
message:@"Password must be less than 15 characters."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}else{
_labelOutput.text = @"Login with FB?";
}
}
- (IBAction)loginTwitter {
// _labelOutput.text = @"Login with Twitter ya?";
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Login Error"
message:@"Please enter your email correctly."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
@end
正如你在那里看到的那样,我用来显示警告框的代码会重复出现。有没有办法把这个类似的代码放在那里,所以只需将Title和Message作为变量来调用它:
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Login Error"
message:@"Please enter your email correctly."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
在PHP世界中,这个方法命名为function,但我不知道Objective-C世界中的名字是什么。非常感谢你。
答案 0 :(得分:4)
要减少代码,为什么不创建另一种方法来显示警告框?
- (void) alertWithTitle:(NSString *)title andMessage:(NSString *)msg
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
然后简单地致电:
[self alertWithTitle:(YOUR TITLE) andMessage:(YOUR MESSAGE)];
答案 1 :(得分:1)
你写了一些方法,你考虑过再写一个吗? E.g。
- (void) showAlertWithTitle:(NSString *)title andMessage:(NSString *)message
{
...
}
然后当您需要警报时调用此方法,例如:
[self showAlertWithTitle:@"Login Error" andMessage:@"Please enter your email correctly."];
HTH