. UIViewController, , , , UITableViewController . , , . , . .h , UIViewController, .
, , , " ". , - NS_REQUIRES_NIL_TERMINATION
, , - , , .
, , . - , , .
#import <Foundation/Foundation.h>
@protocol NumericKeyboardHelperProtocol
- (void)numericDoneButtonPressed:(id)sender;
@end
@interface NumericKeyboardHelper : NSObject
@property (strong, nonatomic) UIButton *numericDoneButton;
@property (strong, nonatomic) NSArray *numericFields;
@property (weak, nonatomic) UIViewController *viewController;
- (void)showNumericKeyboard:(id)sender;
- (void)hideNumericKeyboard:(id)sender;
- (id) initWithObserver: (id) observer andSelector:(SEL)selector andFields:(UIControl *)argList, ... NS_REQUIRES_NIL_TERMINATION;
@end
#import "NumericKeyboardHelper.h"
@implementation NumericKeyboardHelper
@synthesize numericDoneButton=_numericDoneButton;
@synthesize viewController=_viewController;
@synthesize numericFields=_numericFields;
- (id) initWithObserver: (id) observer andSelector:(SEL)selector andFields:(UIControl *)argList, ... NS_REQUIRES_NIL_TERMINATION
{
if (self = [super init])
{
[[NSNotificationCenter defaultCenter] addObserver:observer
selector:selector
name:@"numericDoneButtonPressed"
object:nil];
NSMutableArray *a = [[NSMutableArray alloc]init];
va_list args;
va_start(args, argList);
for (UIControl *arg = argList; arg != nil; arg = va_arg(args, UIControl*))
{
[a addObject:arg];
}
va_end(args);
self.numericFields = [NSArray arrayWithArray:a];
NSLog(@"Array count %i", [a count]);
self.viewController = observer;
[self setAllTextFields:self.viewController.view];
}
return self;
}
- (void) setAllTextFields: (UIView *) view
{
for (UIView *v in view.subviews)
{
if ([v isKindOfClass:[UITextField class]])
{
UITextField *t = (UITextField *)v;
if ([self.numericFields containsObject:v])
[t addTarget:self action:@selector(showNumericKeyboard:) forControlEvents:UIControlEventTouchDown];
else
[t addTarget:self action:@selector(hideNumericKeyboard:) forControlEvents:UIControlEventTouchDown];
}
else if ([v.subviews count] > 0)
{
[self setAllTextFields:v];
}
}
}
- (void)addNumericDoneButtonToKeyboard
{
if (self.numericDoneButton == nil)
{
self.numericDoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.numericDoneButton.frame = CGRectMake(0, 163, 106, 53);
self.numericDoneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0)
{
[self.numericDoneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
[self.numericDoneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
}
else
{
[self.numericDoneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
[self.numericDoneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
}
[self.numericDoneButton addTarget:self action:@selector(numericDoneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
[[self keyboardView] addSubview:self.numericDoneButton];
}
- (void)showNumericKeyboard:(id)sender
{
[self addNumericDoneButtonToKeyboard];
self.numericDoneButton.hidden = NO;
}
- (void)hideNumericKeyboard:(id)sender
{
self.numericDoneButton.hidden = YES;
}
- (UIView *)keyboardView
{
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2 && [[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) || [[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
return keyboard;
}
return nil;
}
- (void)numericDoneButtonPressed:(id)sender
{
for (UIControl *c in self.numericFields)
[c resignFirstResponder];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"numericDoneButtonPressed"
object:sender ];
}
@end