//
// ViewBaseController.h
//
//
// Created by nokiaxjw on 12-12-6.
// Copyright (c) 2012年 All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewBaseController :UIViewController<UITextFieldDelegate>
@property(nonatomic,strong)UITextField *activeTextField;
@property(nonatomic,assign)NSUInteger keyboardHeight;
//UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
- (IBAction)onEditDone:(id)sender;
@end
//
// ViewBaseController.m
// Sb06D
//
// Created by nokiaxjw on 12-12-6.
// Copyright (c) 2012年. All rights reserved.
//
#import "ViewBaseController.h"
#import "AppDelegate.h"
#import "MainViewController.h"
@interface ViewBaseController ()
@end
@implementation ViewBaseController
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [superinitWithCoder:aDecoder];
if (self) {
self.keyboardHeight = 352.0f;
self.activeTextField =nil;
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenterdefaultCenter]removeObserver:self];
}
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
AppDelegate *delegate = (AppDelegate*)[[UIApplicationsharedApplication]delegate];
self.device = delegate.device;
self.pile =self.device.pile;
[selfregisterForKeyboardNotifications];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) registerForKeyboardNotifications
{ [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];
}
#pragma mark -
#pragma mark Responding to keyboard events
- (void)keyboardWillShow:(NSNotification *)notification
{
/*
Reduce the size of the text view so that it's not obscured by the keyboard.
Animate the resize so that it's in sync with the appearance of the keyboard.
*/
NSDictionary *userInfo = [notificationuserInfo];
// Get the origin of the keyboard when it's displayed.
NSValue* aValue = [userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
CGRect keyboardRect = [aValueCGRectValue];
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValuegetValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
if ( [self.viewisKindOfClass:[UIScrollViewclass]] )
{
UIInterfaceOrientation interfaceOrientation = [UIApplicationsharedApplication].statusBarOrientation;
if (interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
self.keyboardHeight = keyboardRect.size.height;
}
else
self.keyboardHeight = keyboardRect.size.width;
UIScrollView *scrollView = (UIScrollView*)self.view;
CGSize contentSize =CGSizeMake(scrollView.bounds.size.width,scrollView.bounds.size.height+self.keyboardHeight);
scrollView.contentSize =contentSize;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect =self.view.bounds;
aRect.size.height -=self.keyboardHeight;
CGPoint pt =self.activeTextField.frame.origin;
if ( pt.y+self.activeTextField.frame.size.height>(aRect.origin.y+aRect.size.height) )
{
CGPoint scrollPoint =CGPointMake(0.0, pt.y+self.activeTextField.frame.size.height-(aRect.origin.y+aRect.size.height));
[scrollViewsetContentOffset:scrollPointanimated:YES];
}
}
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSDictionary* userInfo = [notificationuserInfo];
/*
Restore the size of the text view (fill self's view).
Animate the resize so that it's in sync with the disappearance of the keyboard.
*/
NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValuegetValue:&animationDuration];
if ( [self.viewisKindOfClass:[UIScrollViewclass]] )
{
UIScrollView *scrollView = (UIScrollView*)self.view;
CGSize contentSize = scrollView.bounds.size;
scrollView.contentSize =contentSize;
}
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.activeTextField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.activeTextField =nil;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
self.activeTextField =nil;
return NO;
}
- (IBAction)onEditDone:(id)sender
{
if (self.activeTextField!=nil)
[self.activeTextFieldresignFirstResponder];
}
@end