Moving the view so a Textfield can be seen when keyboard is shown

Found this great article that shows how to scroll the view so that a textfield is in view when the keyboard appears.

http://de-co-de.blogspot.com/2009/03/moving-uitextfield-above-keyboard.html

I’ve copied the code below so that I have a reference to it as well.

You also need to ensure the following steps are followed…

  • Make your controller implement UITextViewDelegate
  • setup your textfield’s (s) delegate to be your controller
  • in the controller @interface add an int verticalOffset
  • in the controller @implementation add the functions below
  • #pragma mark "-- text editing support --"
    // Animate the entire view up or down, to prevent the keyboard from covering the text field.
    - (void)moveView:(int)offset {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];
        // Make changes to the view's frame inside the animation block. They will be animated instead
        // of taking place immediately.
        CGRect rect = self.view.frame;
        rect.origin.y -= offset;
        rect.size.height += offset;
        self.view.frame = rect;
    
        [UIView commitAnimations];
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *) sender {
        [sender resignFirstResponder];
        if (verticalOffset!=0) {
            [self moveView: -verticalOffset];
            verticalOffset = 0;
        }
        return TRUE;
    }
    
    - (void)textFieldDidBeginEditing:(UITextField *)theTextField {
        int wantedOffset = theTextField.frame.origin.y-200;
        if ( wantedOffset < 0 ) { 
            wantedOffset = 0;
        } 
        if ( wantedOffset != verticalOffset ) {
            [self moveView: wantedOffset - verticalOffset];
            verticalOffset = wantedOffset;
        }
    }
    

svn cleanup

I recently had a problem where I couldn’t check in files from my project because I was getting an error stating that the folder was locked. I tried to run a cleanup on the folder but that failed due to the “build” folder not being under source control. Quick and dirty solution to this is move the build folder to a different directory, run the cleanup again, then move the folder back. All worked after that.

Core Data – fetch a single entity

I’ve used the following code to retrieve an entity from Core Data, so that the result returns the entity object rather than a Dictionary

-(Target *) currentTargetInContext:(NSManagedObjectContext *)context {
	Target *match;
	
	NSFetchRequest *request = [[NSFetchRequest alloc] init];
	NSEntityDescription *entity = [NSEntityDescription entityForName: @"Target" inManagedObjectContext:context];
	[request setEntity:entity];
	
	[request setResultType:NSManagedObjectResultType];
	
	NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Achieved == 0 "];
	[request setPredicate:predicate];
	
	NSError *error;
	NSArray *objects = [context executeFetchRequest:request error:&error];
	match = nil;
	if (objects == nil)
	{
		// handle the error
	}
	else
	{
		if ([objects count] > 0)
		{
			match = (Target *)[objects objectAtIndex:0];
		}
	}
	
	[request release];
	
	return match;
}