Skip to content

Commit

Permalink
UPDATE - fixed issue when apps transition to background (inactive) an…
Browse files Browse the repository at this point in the history
…d foreground (active)
  • Loading branch information
stuartcarnie committed Jul 26, 2011
1 parent 9b8058e commit 635b4f0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions iCadeTest/iCade/iCadeReaderView.h
Expand Up @@ -61,5 +61,6 @@

@property (nonatomic, assign) iCadeState iCadeState;
@property (nonatomic, assign) id<iCadeEventDelegate> delegate;
@property (nonatomic, assign) BOOL active;

@end
39 changes: 38 additions & 1 deletion iCadeTest/iCade/iCadeReaderView.m
Expand Up @@ -25,21 +25,58 @@ of this software and associated documentation files (the "Software"), to deal
static const char *ON_STATES = "wdxayhujikol";
static const char *OFF_STATES = "eczqtrfnmpgv";

@interface iCadeReaderView()

- (void)didEnterBackground;
- (void)didBecomeActive;

@end

@implementation iCadeReaderView

@synthesize iCadeState=_iCadeState, delegate=_delegate;
@synthesize iCadeState=_iCadeState, delegate=_delegate, active;

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];

return self;
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[super dealloc];
}

- (void)didEnterBackground {
if (self.active)
[self resignFirstResponder];
}

- (void)didBecomeActive {
if (self.active)
[self becomeFirstResponder];
}

- (BOOL)canBecomeFirstResponder {
return YES;
}

- (void)setActive:(BOOL)value {
if (active == value) return;

active = value;
if (active) {
[self becomeFirstResponder];
} else {
[self resignFirstResponder];
}
}

- (UIView*) inputView {
return inputView;
}
Expand Down
2 changes: 1 addition & 1 deletion iCadeTest/iCadeTestViewController.m
Expand Up @@ -64,7 +64,7 @@ - (void)viewDidLoad
[super viewDidLoad];
iCadeReaderView *control = [[iCadeReaderView alloc] initWithFrame:CGRectZero];
[self.view addSubview:control];
[control becomeFirstResponder];
control.active = YES;
control.delegate = self;
[control release];
_stickCenter = stick.center;
Expand Down

0 comments on commit 635b4f0

Please sign in to comment.