これは XCode を学習する 2 日目なので、ここではまったくの初心者で、次の .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 を variable として指定するだけで呼び出すことができるように、この同様のコードをどこかに配置する方法はありますか?
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 の世界では何という名前かわかりません。どうもありがとうございます。