Saint Robson
Saint Robson

Reputation: 5525

iOS: Simplified UIAlertView Code So It Can Be Used Over And Over Again

this is my second day on learning XCode, so I'm totally newbie here and I have this .m code :

#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

as you can see there, the code I used to display alert box is repeated. Is there any way to put this similar code somewhere up there so it can be called just by giving Title and Message as variable :

    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Login Error"
                                                      message:@"Please enter your email correctly."
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    [message show];

In PHP world, this method named function, but I don't know what's name in Objective-C world. thank you very much.

Upvotes: 0

Views: 242

Answers (2)

CRD
CRD

Reputation: 53010

You have written a handful of methods, have you considered just writing another one? E.g.

- (void) showAlertWithTitle:(NSString *)title andMessage:(NSString *)message
{
    ...
}

and then when you need an alert call this method, e.g.:

[self showAlertWithTitle:@"Login Error" andMessage:@"Please enter your email correctly."];

HTH

Upvotes: 1

Thanh TRAN CONG
Thanh TRAN CONG

Reputation: 293

To reduce your code why dont you create another method to show the AlertBox ?

- (void) alertWithTitle:(NSString *)title andMessage:(NSString *)msg
{
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:title
                                                      message:msg
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    [message show];
}

Then simply call :

[self alertWithTitle:(YOUR TITLE) andMessage:(YOUR MESSAGE)];

Upvotes: 4

Related Questions