Added DTAlertView to display alert view popup

This commit is contained in:
Gautier Pelloux-Prayer 2015-01-05 12:48:52 +01:00
parent 2c39de1374
commit 636bccbabe
3 changed files with 247 additions and 0 deletions

View file

@ -0,0 +1,57 @@
//
// DTAlertView.h
// DTFoundation
//
// Created by Oliver Drobnik on 11/22/12.
// Copyright (c) 2012 Cocoanetics. All rights reserved.
//
#import "DTWeakSupport.h"
// the block to execute when an alert button is tapped
typedef void (^DTAlertViewBlock)(void);
/**
Extends UIAlertView with support for blocks.
*/
@interface DTAlertView : UIAlertView
/**
* Initializes the alert view. Add buttons and their blocks afterwards.
@param title The alert title
@param message The alert message
*/
- (id)initWithTitle:(NSString *)title message:(NSString *)message;
/**
Adds a button to the alert view
@param title The title of the new button.
@param block The block to execute when the button is tapped.
@returns The index of the new button. Button indices start at 0 and increase in the order they are added.
*/
- (NSInteger)addButtonWithTitle:(NSString *)title block:(DTAlertViewBlock)block;
/**
Same as above, but for a cancel button.
@param title The title of the cancel button.
@param block The block to execute when the button is tapped.
@returns The index of the new button. Button indices start at 0 and increase in the order they are added.
*/
- (NSInteger)addCancelButtonWithTitle:(NSString *)title block:(DTAlertViewBlock)block;
/**
Set a block to be run on alertViewCancel:.
@param block The block to execute.
*/
- (void)setCancelBlock:(DTAlertViewBlock)block;
/**
* Use the alertViewDelegate when you want to to receive UIAlertViewDelegate messages.
*/
@property (nonatomic, DT_WEAK_PROPERTY) id<UIAlertViewDelegate> alertViewDelegate;
@end

View file

@ -0,0 +1,184 @@
//
// DTAlertView.m
// DTFoundation
//
// Created by Oliver Drobnik on 11/22/12.
// Copyright (c) 2012 Cocoanetics. All rights reserved.
//
#import "DTAlertView.h"
@interface DTAlertView() <UIAlertViewDelegate>
@end
@implementation DTAlertView
{
NSMutableDictionary *_actionsPerIndex;
DTAlertViewBlock _cancelBlock;
}
- (void)dealloc
{
[super setDelegate:nil];
self.alertViewDelegate = nil;
[super dealloc];
}
// designated initializer
- (id)init
{
self = [super init];
if (self)
{
_actionsPerIndex = [[NSMutableDictionary alloc] init];
[super setDelegate:self];
}
return self;
}
- (id)initWithTitle:(NSString *)title message:(NSString *)message
{
return [self initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
}
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
self = [self init];
if (self)
{
self.title = title;
self.message = message;
if (otherButtonTitles != nil) {
[self addButtonWithTitle:otherButtonTitles];
va_list args;
va_start(args, otherButtonTitles);
NSString *title = nil;
while( (title = va_arg(args, NSString *)) ) {
[self addButtonWithTitle:title];
}
va_end(args);
}
if (cancelButtonTitle) {
[self addCancelButtonWithTitle:cancelButtonTitle block:nil];
}
self.alertViewDelegate = delegate;
}
return self;
}
- (NSInteger)addButtonWithTitle:(NSString *)title block:(DTAlertViewBlock)block
{
NSInteger retIndex = [self addButtonWithTitle:title];
if (block)
{
NSNumber *key = [NSNumber numberWithInteger:retIndex];
[_actionsPerIndex setObject:[block copy] forKey:key];
}
return retIndex;
}
- (NSInteger)addCancelButtonWithTitle:(NSString *)title block:(DTAlertViewBlock)block
{
NSInteger retIndex = [self addButtonWithTitle:title block:block];
[self setCancelButtonIndex:retIndex];
return retIndex;
}
- (void)setCancelBlock:(DTAlertViewBlock)block
{
_cancelBlock = block;
}
# pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSNumber *key = [NSNumber numberWithInteger:buttonIndex];
DTAlertViewBlock block = [_actionsPerIndex objectForKey:key];
if (block)
{
block();
}
if ([self.alertViewDelegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
{
[self.alertViewDelegate alertView:self clickedButtonAtIndex:buttonIndex];
}
}
- (void)alertViewCancel:(UIAlertView *)alertView
{
if (_cancelBlock)
{
_cancelBlock();
}
if ([self.alertViewDelegate respondsToSelector:@selector(alertViewCancel:)])
{
[self.alertViewDelegate alertViewCancel:self];
}
}
- (void)willPresentAlertView:(UIAlertView *)alertView
{
if ([self.alertViewDelegate respondsToSelector:@selector(willPresentAlertView:)])
{
[self.alertViewDelegate willPresentAlertView:self];
}
}
- (void)didPresentAlertView:(UIAlertView *)alertView
{
if ([self.alertViewDelegate respondsToSelector:@selector(didPresentAlertView:)])
{
[self.alertViewDelegate didPresentAlertView:self];
}
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ([self.alertViewDelegate respondsToSelector:@selector(alertView:willDismissWithButtonIndex:)])
{
[self.alertViewDelegate alertView:self willDismissWithButtonIndex:buttonIndex];
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ([self.alertViewDelegate respondsToSelector:@selector(alertView:didDismissWithButtonIndex:)])
{
[self.alertViewDelegate alertView:self didDismissWithButtonIndex:buttonIndex];
}
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if ([self.alertViewDelegate respondsToSelector:@selector(alertViewShouldEnableFirstOtherButton:)])
{
return [self.alertViewDelegate alertViewShouldEnableFirstOtherButton:self];
}
return YES;
}
#pragma mark - Properties
- (void)setDelegate:(id <UIAlertViewDelegate>)delegate
{
if (delegate)
{
NSLog(@"Calling setDelegate is not supported! Use setAlertViewDelegate instead");
}
}
@end

View file

@ -123,6 +123,7 @@
639CEB031A1DF4EB004DE38F /* UICompositeViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 639CEB051A1DF4EB004DE38F /* UICompositeViewController.xib */; };
639CEB061A1DF4F1004DE38F /* UIChatRoomCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 639CEB081A1DF4F1004DE38F /* UIChatRoomCell.xib */; };
639CEB091A1DF4FA004DE38F /* UIChatCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 639CEB0B1A1DF4FA004DE38F /* UIChatCell.xib */; };
63CD4B4F1A5AAC8C00B84282 /* DTAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = 63CD4B4E1A5AAC8C00B84282 /* DTAlertView.m */; };
70571E1A13FABCB000CDD3C2 /* rootca.pem in Resources */ = {isa = PBXBuildFile; fileRef = 70571E1913FABCB000CDD3C2 /* rootca.pem */; };
7066FC0C13E830E400EFC6DC /* libvpx.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7066FC0B13E830E400EFC6DC /* libvpx.a */; };
70E542F313E147E3002BA2C0 /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 70E542F213E147E3002BA2C0 /* OpenGLES.framework */; };
@ -985,6 +986,8 @@
639CEB0A1A1DF4FA004DE38F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/UIChatCell.xib; sourceTree = "<group>"; };
639CEB0C1A1DF528004DE38F /* fr */ = {isa = PBXFileReference; fileEncoding = 2483028224; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/UICallCell.strings; sourceTree = "<group>"; };
639CEB0D1A1DF52C004DE38F /* ru */ = {isa = PBXFileReference; fileEncoding = 2483028224; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/UICallCell.strings; sourceTree = "<group>"; };
63CD4B4D1A5AAC8C00B84282 /* DTAlertView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DTAlertView.h; sourceTree = "<group>"; };
63CD4B4E1A5AAC8C00B84282 /* DTAlertView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DTAlertView.m; sourceTree = "<group>"; };
63EF7FDC1A24B5810017A416 /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/AboutViewController.strings; sourceTree = "<group>"; };
70571E1913FABCB000CDD3C2 /* rootca.pem */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = rootca.pem; path = "liblinphone-sdk/apple-darwin/share/linphone/rootca.pem"; sourceTree = "<group>"; };
7066FC0B13E830E400EFC6DC /* libvpx.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libvpx.a; path = "liblinphone-sdk/apple-darwin/lib/libvpx.a"; sourceTree = "<group>"; };
@ -2278,6 +2281,8 @@
isa = PBXGroup;
children = (
D37EE160160377D7003608A6 /* DTActionSheet.h */,
63CD4B4D1A5AAC8C00B84282 /* DTAlertView.h */,
63CD4B4E1A5AAC8C00B84282 /* DTAlertView.m */,
D37EE161160377D7003608A6 /* DTActionSheet.m */,
F0642EF719DAF32E009DB336 /* DTWeakSupport.h */,
);
@ -3810,6 +3815,7 @@
D35E759F159460B70066B1C1 /* SettingsViewController.m in Sources */,
F03CA84318C72F1A0008889D /* UITextViewNoDefine.m in Sources */,
D37DC6C11594AE1800B2A5EB /* LinphoneCoreSettingsStore.m in Sources */,
63CD4B4F1A5AAC8C00B84282 /* DTAlertView.m in Sources */,
D3EA53FD159850E80037DC6B /* LinphoneManager.m in Sources */,
D3EA540D1598528B0037DC6B /* ChatTableViewController.m in Sources */,
D3EA5411159853750037DC6B /* UIChatCell.m in Sources */,