Thursday, September 16, 2010

Keyboard Delegate Methods and How to call application delegate methods on another controller.

Code for UITextField Delegate Methods. In this example keyboard will show and hide with animations.


Code for .h file.


#import
#import "ProjectNameiPadAppDelegate.h"

@interface UIParentalInformationView : UIViewController {

    SportsManagementSystemiPadAppDelegate *appDelegate;
    
    IBOutlet UITextField *txtUserNameFather;
    IBOutlet UITextField *txtFirstNameFather;
    IBOutlet UITextField *txtLastNameFather;
    IBOutlet UITextField *txtDOBFather;
    IBOutlet UITextField *txtCityFather;
    IBOutlet UITextField *txtStateFather;
    IBOutlet UITextField *txtCountryFather;
   
    IBOutlet UIButton *btnSave;
    IBOutlet UIButton *btnBack;
}

-(IBAction)btnSave_TouchUpInside:(id)sender;
-(IBAction)btnBack_TouchUpInside:(id)sender;
-(void)setViewMovedUp:(BOOL)movedUp;
@end

 



Code for .m file.


 #import "UIControllerNameView.h"

#define kOFFSET_FOR_KEYBOARD 220.0

@implementation UIParentalInformationView


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
   
                                                               
}

-(IBAction)btnSave_TouchUpInside:(id)sender{

    if (![txtUserNameFather.text isEqualToString:@""] && ![txtFirstNameFather.text isEqualToString:@""] && ![txtLastNameFather.text isEqualToString:@""] && ![txtDOBFather.text isEqualToString:@""] && ![txtCityFather.text isEqualToString:@""] && ![txtStateFather.text isEqualToString:@""] && ![txtCountryFather.text isEqualToString:@""]) {
        appDelegate = (SportsManagementSystemiPadAppDelegate*)[[UIApplication sharedApplication]delegate];
        appDelegate.arrFamilyInfo = [[NSMutableArray alloc]initWithObjects:txtUserNameFather.text, txtFirstNameFather.text, txtLastNameFather.text,
                                     txtDOBFather.text, txtCityFather.text, txtStateFather.text, txtCountryFather.text, nil];
        [appDelegate.arrFamilyInfo retain];
       
        [self.navigationController popViewControllerAnimated:YES];
    }
    else {
        UIAlertView *alearView = [[UIAlertView alloc]initWithTitle:@"Required!" message:@"Enter Father's Info." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alearView show];
        [alearView release];
    }   
}
/*
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    //UITextField *_textField = (UITextField *)sender;
   
    if (([sender tag] == 55) || ([sender tag] == 66) || ([sender tag] == 77))
    {
        //move the main view, so that the keyboard does not hide it.
            [self setViewMovedUp:YES];
    }
   
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    if (([textField tag] == 55) || ([textField tag] == 66) || ([textField tag] == 77))
    {
        //move the main view, so that the keyboard does not hide it.
        [self setViewMovedUp:NO];
    }
    return YES;
}
*/
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5]; // if you want to slide up the view
   
    CGRect rect = self.view.frame;
    if (movedUp)
    {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard
        // 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        //rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        // revert back to the normal state.
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        //rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.view.frame = rect;
   
    [UIView commitAnimations];
}
-(void)keyboardWillHide:(NSNotification *)notif{

    [self setViewMovedUp:NO];   
}

- (void)keyboardWillShow:(NSNotification *)notif
{   

    [self setViewMovedUp:YES];
}


- (void)viewWillAppear:(BOOL)animated
{
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:self.view.window];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:self.view.window];
   
}

- (void)viewWillDisappear:(BOOL)animated
{
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
   
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


- (void)setView:(UIView*)view MovedUp:(BOOL)movedUp{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    CGRect rect = view.frame;
    if (movedUp)
    {
        rect.origin.y = (self.view.frame.size.height + self.view.frame.origin.y) - view.frame.size.height;
    }
    else
    {
        rect.origin.y = (self.view.frame.size.height + self.view.frame.origin.y) + view.frame.size.height;
    }
    view.frame = rect;
   
    [UIView commitAnimations];
}

-(IBAction)btnBack_TouchUpInside:(id)Sender{

    [self.navigationController popViewControllerAnimated:YES];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];

}


- (void)dealloc {
    [super dealloc];
}


@end

No comments:

Post a Comment