/
UIViewController+MBProgressHUD.m
126 lines (104 loc) · 3.04 KB
/
UIViewController+MBProgressHUD.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* UIViewController+MBProgressHUD.m
*
* Created by Adam Duke on 10/20/11.
* Copyright 2011 appRenaissance, LLC. All rights reserved.
*
*/
#import "MBProgressHUD.h"
#import "UIViewController+MBProgressHUD.h"
#import <objc/runtime.h>
/* This key is used to dynamically create an instance variable
* within the MBProgressHUD category using objc_setAssociatedObject
*/
const void *progressHUDKey = &progressHUDKey;
/* This key is used to dynamically create an instance variable
* within the MBProgressHUD category using objc_setAssociatedObject
*/
const void *finishedHandlerKey = &finishedHandlerKey;
@interface UIViewController (MBProgressHUD_Private)
@property (nonatomic, copy) HUDFinishedHandler finishedHandler;
@end
@implementation UIViewController (MBProgressHUD_Private)
- (HUDFinishedHandler)finishedHandler
{
HUDFinishedHandler block = objc_getAssociatedObject(self, finishedHandlerKey);
return block;
}
- (void)setFinishedHandler:(HUDFinishedHandler)completionBlock
{
objc_setAssociatedObject(self, finishedHandlerKey, completionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
@end
@implementation UIViewController (MBProgressHUD)
- (MBProgressHUD *)progressHUD
{
MBProgressHUD *hud = objc_getAssociatedObject(self, progressHUDKey);
if(!hud)
{
UIView *hudSuperView = self.view;
hud = [[[MBProgressHUD alloc] initWithView:hudSuperView] autorelease];
hud.dimBackground = YES;
hud.removeFromSuperViewOnHide = YES;
[hudSuperView addSubview:hud];
self.progressHUD = hud;
}
return hud;
}
- (void)setProgressHUD:(MBProgressHUD *)progressHUD
{
objc_setAssociatedObject(self, progressHUDKey, progressHUD, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)_showHUDWithMessage:(NSString *)message
{
self.progressHUD.labelText = message;
if(self.progressHUD.taskInProgress)
{
return;
}
self.progressHUD.taskInProgress = YES;
[self.progressHUD show:YES];
}
- (void)showHUD
{
[self _showHUDWithMessage:nil];
}
- (void)showHUDWithMessage:(NSString *)message
{
[self _showHUDWithMessage:message];
}
- (void)hideHUD
{
if(!self.progressHUD.taskInProgress)
{
return;
}
self.progressHUD.taskInProgress = NO;
[self.progressHUD hide:YES];
self.progressHUD = nil;
}
- (void)hideHUDWithCompletionMessage:(NSString *)message afterDelay:(NSTimeInterval)delay
{
self.progressHUD.labelText = message;
[self performSelector:@selector(hideHUD) withObject:nil afterDelay:delay];
}
- (void)hideHUDWithCompletionMessage:(NSString *)message
{
[self hideHUDWithCompletionMessage:message afterDelay:1];
}
- (void)hideHUDWithCompletionMessage:(NSString *)message finishedHandler:(HUDFinishedHandler)finishedHandler
{
self.progressHUD.delegate = self;
self.finishedHandler = finishedHandler;
[self hideHUDWithCompletionMessage:message];
}
- (void)hudWasHidden:(MBProgressHUD *)hud
{
if(self.finishedHandler)
{
self.finishedHandler();
self.finishedHandler = nil;
}
self.progressHUD.delegate = nil;
}
@end