-1

I am a beginner to IOS programming (and programming in general) and I simply don't understand why my code does not shift everything on my screen when the keyboard appears on the screen, as I intend it to. Can someone please help me understand what I am missing here?

ViewController.h

     @interface ViewController : UIViewController <UITextFieldDelegate> { 
} 
@property (strong, nonatomic) IBOutlet UITextField *firstRoommateTextField;
@property (strong, nonatomic) IBOutlet UITextField *secondRoommateTextField;
@property (strong, nonatomic) IBOutlet UILabel *calculatedValueLabel;


- (IBAction)calculateButton:(UIButton *)sender;
- (IBAction)textFieldDismiss2:(id)sender;
- (IBAction)textFieldDismiss1:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize firstRoommateTextField = _firstRoommateTextField;
@synthesize secondRoommateTextField = _secondRoommateTextField;

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    const int movementDistance = 210; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed

    int movement = (up ? -movementDistance : movementDistance);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self animateTextField: textField up: NO];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
Shafiq
  • 1
  • 1
  • 2
  • If you're just starting out, asking broad questions like this on Stack Overflow is not the place you need to be. You should find a good book or a series of online tutorials. Have a look at [Good resources for learning ObjC](http://stackoverflow.com/q/1374660). The Big Nerd Ranch books are excellent, and lots of people like the Stanford iOS course on iTunes U. Good luck! – jscs Feb 17 '15 at 09:07

2 Answers2

0

First check the textfield object is assigned to the delegate..i.e. _firstRoommateTextField.delegate = self; _secondRoommateTextField.delegate = self;

and then check your delegate methods are getting called or not.

Finally try this, instead of CGRectOffset, try CGRectMake.

RJV Kumar
  • 2,408
  • 1
  • 19
  • 28
0

I think you forgot to set the delegate for text field which you can set as _firstRoommateTextField.delegate = self; _secondRoommateTextField.delegate = self; in viewDidLoad

or from storyBoard ctrl+drag from textField to ViewController (right top just below View Controller scene ) and select delegate .

Saket Kumar
  • 1,157
  • 2
  • 14
  • 30