diff --git a/SampleApp/SAViewController.h b/SampleApp/SAViewController.h index 0b89461..e616441 100644 --- a/SampleApp/SAViewController.h +++ b/SampleApp/SAViewController.h @@ -8,7 +8,11 @@ #import -@interface SAViewController : UIViewController +@interface SAViewController : UIViewController { + NSString *code; +} + +@property (atomic, copy) NSString *code; - (IBAction)setPIN; - (IBAction)checkPIN; diff --git a/SampleApp/SAViewController.m b/SampleApp/SAViewController.m index 4b9dca0..9ec500e 100644 --- a/SampleApp/SAViewController.m +++ b/SampleApp/SAViewController.m @@ -12,18 +12,31 @@ @implementation SAViewController +@synthesize code; + +- (void)viewDidLoad { + // Set default code + self.code = @"0187"; +} + - (IBAction)setPIN { GCPINViewController *PIN = [[GCPINViewController alloc] initWithNibName:nil bundle:nil mode:GCPINViewControllerModeCreate]; PIN.messageText = @"Enter a passcode"; + PIN.confirmText = @"Confirm your passcode"; PIN.errorText = @"The passcodes do not match"; PIN.title = @"Set Passcode"; - PIN.verifyBlock = ^(NSString *code) { - NSLog(@"setting code: %@", code); + PIN.verifyBlock = ^(NSString *newCode) { + NSLog(@"setting code: %@", newCode); + self.code = newCode; return YES; }; + PIN.cancelBlock = ^ { + NSLog(@"Cancelling PIN setting"); + }; + PIN.cancelButtonVisible = YES; [PIN presentFromViewController:self animated:YES]; [PIN release]; } @@ -36,10 +49,14 @@ - (IBAction)checkPIN { PIN.messageText = @"Enter your passcode"; PIN.errorText = @"Incorrect passcode"; PIN.title = @"Enter Passcode"; - PIN.verifyBlock = ^(NSString *code) { - NSLog(@"checking code: %@", code); - return [code isEqualToString:@"0187"]; + PIN.verifyBlock = ^(NSString *enteredCode) { + NSLog(@"checking code: %@", enteredCode); + return [enteredCode isEqualToString:self.code]; + }; + PIN.cancelBlock = ^ { + NSLog(@"Cancelling verification"); }; + PIN.cancelButtonVisible = YES; [PIN presentFromViewController:self animated:YES]; [PIN release]; } diff --git a/pinview/GCPINViewController.h b/pinview/GCPINViewController.h index 437a5db..72c4f88 100644 --- a/pinview/GCPINViewController.h +++ b/pinview/GCPINViewController.h @@ -29,6 +29,7 @@ typedef enum { } GCPINViewControllerMode; typedef BOOL (^GCPasscodeVerifyBlock) (NSString *code); +typedef void (^GCPasscodeCancelBlock) (); /* @@ -49,6 +50,12 @@ typedef BOOL (^GCPasscodeVerifyBlock) (NSString *code); */ @property (nonatomic, copy) NSString *messageText; +/* + + Set the text to display below the input area when confirming a set PIN + */ +@property (nonatomic, copy) NSString *confirmText; + /* Set the text to display below the input area when the passcode fails @@ -70,6 +77,13 @@ typedef BOOL (^GCPasscodeVerifyBlock) (NSString *code); */ @property (nonatomic, copy) GCPasscodeVerifyBlock verifyBlock; +/* + + Called when cancelling out of passcode entry or verification. + + */ +@property (nonatomic, copy) GCPasscodeCancelBlock cancelBlock; + /* Refer to `GCPINViewControllerMode`. This can only be set through the @@ -103,5 +117,7 @@ typedef BOOL (^GCPasscodeVerifyBlock) (NSString *code); @property (nonatomic, retain) IBOutlet UILabel *messageLabel; @property (nonatomic, retain) IBOutlet UILabel *errorLabel; @property (nonatomic, retain) IBOutlet UITextField *inputField; +@property (nonatomic, retain) IBOutlet UIBarButtonItem *cancelButton; +@property (nonatomic, assign) BOOL cancelButtonVisible; @end diff --git a/pinview/GCPINViewController.m b/pinview/GCPINViewController.m index 4c84ff7..e54360a 100644 --- a/pinview/GCPINViewController.m +++ b/pinview/GCPINViewController.m @@ -46,11 +46,15 @@ @implementation GCPINViewController @synthesize errorLabel = __errorLabel; @synthesize inputField = __inputField; @synthesize messageText = __messageText; +@synthesize confirmText = __confirmText; @synthesize errorText = __errorText; @synthesize labels = __labels; @synthesize mode = __mode; @synthesize text = __text; @synthesize verifyBlock = __verifyBlock; +@synthesize cancelBlock = __cancelBlock; +@synthesize cancelButton = __cancelButton; +@synthesize cancelButtonVisible = __cancelButtonVisible; #pragma mark - object methods - (id)initWithNibName:(NSString *)nib bundle:(NSBundle *)bundle mode:(GCPINViewControllerMode)mode { @@ -85,10 +89,13 @@ - (void)dealloc { self.errorLabel = nil; self.inputField = nil; self.messageText = nil; + self.confirmText = nil; self.errorText = nil; self.labels = nil; self.text = nil; self.verifyBlock = nil; + self.cancelBlock = nil; + self.cancelButton = nil; // super [super dealloc]; @@ -129,11 +136,22 @@ - (void)dismiss { [[UIApplication sharedApplication] endIgnoringInteractionEvents]; }); } +- (void)cancel { + __dismiss = YES; + if ( self.cancelBlock != nil ) { + self.cancelBlock(); + } + [self dismissModalViewControllerAnimated:YES]; +} #pragma mark - view lifecycle - (void)viewDidLoad { [super viewDidLoad]; - + + // wire up cancel button + self.cancelButton.target = self; + self.cancelButton.action = @selector(cancel); + // setup labels list self.labels = [NSArray arrayWithObjects: self.fieldOneLabel, @@ -158,6 +176,11 @@ - (void)viewDidLoad { [self.inputField becomeFirstResponder]; } + +- (void)viewWillAppear:(BOOL)animated { + self.navigationItem.leftBarButtonItem = self.cancelButtonVisible ? self.cancelButton : nil; +} + - (void)viewDidUnload { [super viewDidUnload]; self.fieldOneLabel = nil; @@ -169,6 +192,7 @@ - (void)viewDidUnload { self.inputField = nil; self.labels = nil; self.text = nil; + self.cancelButton = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { @@ -190,6 +214,10 @@ - (void)setErrorText:(NSString *)text { __errorText = [text copy]; self.errorLabel.text = __errorText; } +- (void)setCancelButtonVisible:(BOOL)cancelButtonVisible { + __cancelButtonVisible = cancelButtonVisible; + self.navigationItem.leftBarButtonItem = cancelButtonVisible ? self.cancelButton : nil; +} #pragma mark - text field methods - (void)textDidChange:(NSNotification *)notif { @@ -200,6 +228,12 @@ - (void)textDidChange:(NSNotification *)notif { if (self.mode == GCPINViewControllerModeCreate) { if (self.text == nil) { self.text = self.inputField.text; + + // Display confirm text if it's been set + if ( self.confirmText != nil ) { + self.messageLabel.text = self.confirmText; + } + [self resetInput]; } else { @@ -207,7 +241,10 @@ - (void)textDidChange:(NSNotification *)notif { self.verifyBlock(self.inputField.text)) { [self dismiss]; } - else { + else { + // Reinstate original message text + self.messageLabel.text = self.messageText; + [self wrong]; } } diff --git a/pinview/GCPINViewController~ipad.xib b/pinview/GCPINViewController~ipad.xib index 37c54ee..e1a1293 100644 --- a/pinview/GCPINViewController~ipad.xib +++ b/pinview/GCPINViewController~ipad.xib @@ -12,6 +12,7 @@ IBUITextField + IBUIBarButtonItem IBUIImageView IBUIView IBUILabel @@ -240,7 +241,6 @@ {{20, 136}, {280, 21}} - NO YES 7 @@ -331,6 +331,11 @@ IBIPadFramework + + IBIPadFramework + 1 + 1 + @@ -398,6 +403,14 @@ 33 + + + cancelButton + + + + 35 + @@ -499,6 +512,11 @@ + + 34 + + + @@ -519,12 +537,13 @@ com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin - 33 + 35 @@ -532,6 +551,7 @@ GCPINViewController UIViewController + UIBarButtonItem UILabel UILabel UILabel @@ -541,6 +561,10 @@ UILabel + + cancelButton + UIBarButtonItem + errorLabel UILabel diff --git a/pinview/GCPINViewController~iphone.xib b/pinview/GCPINViewController~iphone.xib index 0daf054..b7a3390 100644 --- a/pinview/GCPINViewController~iphone.xib +++ b/pinview/GCPINViewController~iphone.xib @@ -13,6 +13,7 @@ YES IBUILabel + IBUIBarButtonItem IBUIImageView IBUIView IBUITextField @@ -47,7 +48,6 @@ {{95, 245}, {97, 31}} - NO YES IBCocoaTouchFramework @@ -384,6 +384,11 @@ AAgACAAIAAEAAQABAAE IBCocoaTouchFramework + + IBCocoaTouchFramework + 1 + 1 + @@ -452,6 +457,14 @@ AAgACAAIAAEAAQABAAE 58 + + + cancelButton + + + + 62 + @@ -558,6 +571,11 @@ AAgACAAIAAEAAQABAAE + + 61 + + + @@ -580,6 +598,7 @@ AAgACAAIAAEAAQABAAE 50.IBPluginDependency 51.IBPluginDependency 52.IBPluginDependency + 61.IBPluginDependency 9.IBPluginDependency @@ -601,6 +620,7 @@ AAgACAAIAAEAAQABAAE com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin @@ -615,7 +635,7 @@ AAgACAAIAAEAAQABAAE - 58 + 62 @@ -627,6 +647,7 @@ AAgACAAIAAEAAQABAAE YES YES + cancelButton errorLabel fieldFourLabel fieldOneLabel @@ -637,6 +658,7 @@ AAgACAAIAAEAAQABAAE YES + UIBarButtonItem UILabel UILabel UILabel @@ -650,6 +672,7 @@ AAgACAAIAAEAAQABAAE YES YES + cancelButton errorLabel fieldFourLabel fieldOneLabel @@ -660,6 +683,10 @@ AAgACAAIAAEAAQABAAE YES + + cancelButton + UIBarButtonItem + errorLabel UILabel