Skip to content

Commit

Permalink
ARC compatibility + EXC_BAD_ACCESS correction
Browse files Browse the repository at this point in the history
- Added support for ARC
- Still compatible with non ARC projects
- Notification observer for interface orientation moved to the
removeFromSuperview method. (it could cause an EXC_BAD_ACCESS otherwise)
- Remove one deviceOrientationDidChange: prototype definition
  • Loading branch information
renebigot committed Jan 3, 2012
1 parent f4e551a commit 537d279
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 13 deletions.
26 changes: 21 additions & 5 deletions MBProgressHUD.h 100644 → 100755
Expand Up @@ -104,8 +104,12 @@ typedef enum {

float progress;

#if __has_feature(objc_arc)
id<MBProgressHUDDelegate> __weak delegate;
#else
id<MBProgressHUDDelegate> delegate;
NSString *labelText;
#endif
NSString *labelText;
NSString *detailsLabelText;
float opacity;
UIFont *labelFont;
Expand Down Expand Up @@ -165,8 +169,11 @@ typedef enum {
* The UIView (i.g., a UIIMageView) to be shown when the HUD is in MBProgressHUDModeCustomView.
* For best results use a 37 by 37 pixel view (so the bounds match the build in indicator bounds).
*/
#if __has_feature(objc_arc)
@property (strong) UIView *customView;
#else
@property (retain) UIView *customView;

#endif
/**
* MBProgressHUD operation mode. Switches between indeterminate (MBProgressHUDModeIndeterminate) and determinate
* progress (MBProgressHUDModeDeterminate). The default is MBProgressHUDModeIndeterminate.
Expand All @@ -187,8 +194,11 @@ typedef enum {
* delegate should conform to the MBProgressHUDDelegate protocol and implement the hudWasHidden method. The delegate
* object will not be retained.
*/
#if __has_feature(objc_arc)
@property (weak) id<MBProgressHUDDelegate> delegate;
#else
@property (assign) id<MBProgressHUDDelegate> delegate;

#endif
/**
* An optional short message to be displayed below the activity indicator. The HUD is automatically resized to fit
* the entire text. If the text is too long it will get clipped by displaying "..." at the end. If left unchanged or
Expand Down Expand Up @@ -267,13 +277,19 @@ typedef enum {
/**
* Font to be used for the main label. Set this property if the default is not adequate.
*/
#if __has_feature(objc_arc)
@property (strong) UIFont* labelFont;
#else
@property (retain) UIFont* labelFont;

#endif
/**
* Font to be used for the details label. Set this property if the default is not adequate.
*/
#if __has_feature(objc_arc)
@property (strong) UIFont* detailsLabelFont;
#else
@property (retain) UIFont* detailsLabelFont;

#endif
/**
* The progress of the progress indicator, from 0.0 to 1.0. Defaults to 0.0.
*/
Expand Down
59 changes: 51 additions & 8 deletions MBProgressHUD.m 100644 → 100755
Expand Up @@ -19,19 +19,26 @@ - (void)handleGraceTimer:(NSTimer *)theTimer;
- (void)handleMinShowTimer:(NSTimer *)theTimer;
- (void)setTransformForCurrentOrientation:(BOOL)animated;
- (void)cleanUp;
- (void)deviceOrientationDidChange:(NSNotification*)notification;
- (void)launchExecution;
- (void)deviceOrientationDidChange:(NSNotification *)notification;
- (void)hideDelayed:(NSNumber *)animated;
- (void)launchExecution;
- (void)cleanUp;

#if __has_feature(objc_arc)
@property (strong) UIView *indicator;
@property (strong) NSTimer *graceTimer;
@property (strong) NSTimer *minShowTimer;
@property (strong) NSDate *showStarted;
#else
@property (retain) UIView *indicator;
@property (assign) float width;
@property (assign) float height;
@property (retain) NSTimer *graceTimer;
@property (retain) NSTimer *minShowTimer;
@property (retain) NSDate *showStarted;
#endif

@property (assign) float width;
@property (assign) float height;

@end

Expand Down Expand Up @@ -149,14 +156,18 @@ - (float)progress {

- (void)updateLabelText:(NSString *)newText {
if (labelText != newText) {
#if !__has_feature(objc_arc)
[labelText release];
#endif
labelText = [newText copy];
}
}

- (void)updateDetailsLabelText:(NSString *)newText {
if (detailsLabelText != newText) {
#if !__has_feature(objc_arc)
[detailsLabelText release];
#endif
detailsLabelText = [newText copy];
}
}
Expand All @@ -171,13 +182,22 @@ - (void)updateIndicators {
}

if (mode == MBProgressHUDModeDeterminate) {
#if __has_feature(objc_arc)
self.indicator = [[MBRoundProgressView alloc] init];
#else
self.indicator = [[[MBRoundProgressView alloc] init] autorelease];
}
#endif
}
else if (mode == MBProgressHUDModeCustomView && self.customView != nil){
self.indicator = self.customView;
} else {
#if __has_feature(objc_arc)
self.indicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
#else
self.indicator = [[[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
#endif
[(UIActivityIndicatorView *)indicator startAnimating];
}

Expand All @@ -200,7 +220,11 @@ + (MBProgressHUD *)showHUDAddedTo:(UIView *)view animated:(BOOL)animated {
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
[view addSubview:hud];
[hud show:animated];
#if __has_feature(objc_arc)
return hud;
#else
return [hud autorelease];
#endif
}

+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated {
Expand Down Expand Up @@ -245,6 +269,15 @@ - (id)initWithView:(UIView *)view {
return me;
}

- (void)removeFromSuperview {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceOrientationDidChangeNotification
object:nil];

[super removeFromSuperview];
}


- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
Expand Down Expand Up @@ -287,9 +320,8 @@ - (id)initWithFrame:(CGRect)frame {
return self;
}

#if !__has_feature(objc_arc)
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];

[indicator release];
[label release];
[detailsLabel release];
Expand All @@ -301,6 +333,7 @@ - (void)dealloc {
[customView release];
[super dealloc];
}
#endif

#pragma mark -
#pragma mark Layout
Expand Down Expand Up @@ -484,9 +517,14 @@ - (void)handleMinShowTimer:(NSTimer *)theTimer {
- (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated {

methodForExecution = method;
#if __has_feature(objc_arc)
targetForExecution = target;
objectForExecution = object;
#else
targetForExecution = [target retain];
objectForExecution = [object retain];

#endif

// Launch execution in new thread
taskInProgress = YES;
[NSThread detachNewThreadSelector:@selector(launchExecution) toTarget:self withObject:nil];
Expand All @@ -496,16 +534,19 @@ - (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object
}

- (void)launchExecution {
#if !__has_feature(objc_arc)
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

#endif
// Start executing the requested task
[targetForExecution performSelector:methodForExecution withObject:objectForExecution];

// Task completed, update view in main thread (note: view operations should
// be done only in the main thread)
[self performSelectorOnMainThread:@selector(cleanUp) withObject:nil waitUntilDone:NO];

#if !__has_feature(objc_arc)
[pool release];
#endif
}

- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void*)context {
Expand Down Expand Up @@ -536,8 +577,10 @@ - (void)cleanUp {

self.indicator = nil;

#if !__has_feature(objc_arc)
[targetForExecution release];
[objectForExecution release];
#endif

[self hide:useAnimation];
}
Expand Down

0 comments on commit 537d279

Please sign in to comment.