Skip to content

Commit

Permalink
Change the protocol methods signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinh Quan committed Oct 14, 2014
1 parent d4cf38b commit 2b761b5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 24 deletions.
30 changes: 20 additions & 10 deletions DQAlertView/DQAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,8 @@

#import <UIKit/UIKit.h>

@protocol DQAlertViewDelegate <NSObject>

@optional
- (void) DQAlertViewDidAppear;

- (void) DQAlertViewCancelButtonClicked;
- (void) DQAlertViewOtherButtonClicked;

@end
@protocol DQAlertViewDelegate;

typedef enum
{
Expand Down Expand Up @@ -132,9 +125,14 @@ typedef void (^DQAlertViewBlock)(void);

#pragma mark - Public Methods

// Init method
// Initialize method, same as UIAlertView
// On the current version, the alert does not support more than one other buttons
// If you pass the title by nil, the alert will be no title. If you pass the otherButtonTitle by nil, the alert will only have cancel button. You can remove all buttons by set all buton titles to nil.
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<DQAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;

// Initialize convenience method
// If you pass the title by nil, the alert will be no title. If you pass the otherButtonTitle by nil, the alert will only have cancel button. You can remove all buttons by set all buton titles to nil.
- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle;
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle;


// You can use this methods instead of calling these properties:
Expand All @@ -157,3 +155,15 @@ typedef void (^DQAlertViewBlock)(void);
- (void)dismiss;

@end

// DQAlertViewDelegate
@protocol DQAlertViewDelegate <NSObject>

@optional
- (void)willAppearAlertView:(DQAlertView *)alertView;
- (void)didAppearAlertView:(DQAlertView *)alertView;

- (void)cancelButtonClickedOnAlertView:(DQAlertView *)alertView;
- (void)otherButtonClickedOnAlertView:(DQAlertView *)alertView;

@end
55 changes: 45 additions & 10 deletions DQAlertView/DQAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,33 @@ - (id)initWithFrame:(CGRect)frame
}

// Init method
- (id) initWithTitle:(NSString *) title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<DQAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
NSString *firstOtherButtonTitle;

va_list args;
va_start(args, otherButtonTitles);
for (NSString *arg = otherButtonTitles; arg != nil; arg = va_arg(args, NSString*))
{
//do something with nsstring
if (!firstOtherButtonTitle) {
firstOtherButtonTitle = arg;
break;
}
}
va_end(args);

if ([self initWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitle:otherButtonTitles]) {
self.delegate = delegate;

return self;
}

return nil;
}

// Init method shorter version
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle
{
self.width = DEFAULT_ALERT_WIDTH;
self.height = DEFAULT_ALERT_HEIGHT;
Expand Down Expand Up @@ -85,7 +111,7 @@ - (id) initWithTitle:(NSString *) title message:(NSString *)message cancelButton
}

// Show in specified view
- (void) showInView: (UIView *) view
- (void)showInView:(UIView *)view
{
[self calculateFrame];
[self setupViews];
Expand All @@ -104,12 +130,14 @@ - (void) showInView: (UIView *) view
[blackOpaqueView addGestureRecognizer:outsideTapGesture];
[view addSubview:blackOpaqueView];
}

[self willAppearAlertView];

[self addThisViewToView:view];
}

// Show in window
- (void) show
- (void)show
{
[self calculateFrame];
[self setupViews];
Expand Down Expand Up @@ -559,8 +587,8 @@ - (void) cancelButtonClicked: (id) sender
self.cancelButtonAction();
}

if ([self.delegate respondsToSelector:@selector(DQAlertViewCancelButtonClicked)]) {
[self.delegate DQAlertViewCancelButtonClicked];
if ([self.delegate respondsToSelector:@selector(cancelButtonClickedOnAlertView:)]) {
[self.delegate cancelButtonClickedOnAlertView:self];
}
}

Expand All @@ -585,15 +613,22 @@ - (void) otherButtonClicked: (id) sender
self.otherButtonAction();
}

if ([self.delegate respondsToSelector:@selector(DQAlertViewOtherButtonClicked)]) {
[self.delegate DQAlertViewOtherButtonClicked];
if ([self.delegate respondsToSelector:@selector(otherButtonClickedOnAlertView:)]) {
[self.delegate otherButtonClickedOnAlertView:self];
}
}

- (void)didAppearAlertView
{
if ([self.delegate respondsToSelector:@selector(didAppearAlertView:)]) {
[self.delegate didAppearAlertView:self];
}
}

- (void) didAppearAlertView
- (void)willAppearAlertView
{
if ([self.delegate respondsToSelector:@selector(DQAlertViewDidAppear)]) {
[self.delegate DQAlertViewDidAppear];
if ([self.delegate respondsToSelector:@selector(willAppearAlertView:)]) {
[self.delegate willAppearAlertView:self];
}
}

Expand Down
21 changes: 17 additions & 4 deletions DQAlertViewDemo/DQAlertViewDemo/DQViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ - (void)didReceiveMemoryWarning

- (IBAction)shortMessage:(id)sender
{
DQAlertView * alertView = [[DQAlertView alloc] initWithTitle:sampleTitle message:shortSampleMessage cancelButtonTitle:@"Cancel" otherButtonTitle:@"OK"];
DQAlertView * alertView = [[DQAlertView alloc] initWithTitle:sampleTitle message:sampleMessage delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK"];

alertView.delegate = self;
[alertView show];
}

Expand Down Expand Up @@ -211,11 +210,25 @@ - (IBAction)handleAction:(id)sender
// }];
}

-(void)DQAlertViewCancelButtonClicked {
#pragma mark - DQAlertViewDelegate

- (void)willAppearAlertView:(DQAlertView *)alertView
{
NSLog(@"Alert View Will Appear");
}


- (void)didAppearAlertView:(DQAlertView *)alertView
{
NSLog(@"Alert View Did Appear");
}

- (void)cancelButtonClickedOnAlertView:(DQAlertView *)alertView {
NSLog(@"Cancel Clicked");
}

-(void) DQAlertViewOtherButtonClicked {
- (void)otherButtonClickedOnAlertView:(DQAlertView *)alertView {
NSLog(@"OK Clicked");
}

@end

0 comments on commit 2b761b5

Please sign in to comment.