Programming

Nov 162009

Here is a handy tip that I came across. If you need to close the keyboard after a user enters the details in the text field all you have to do is addto your .h file and add this code to your .m file.

Add this to your .h file:

@interface WhatEverViewController : UIViewController {
IBOutlet UITextField * firstTextField;
IBOutlet UITextField * secondTextField;
IBOutlet UITextField * doneButtonPressed;
}

@property (nonatomic, retain) IBOutlet UITextField * firstTextField;
@property (nonatomic, retain) IBOutlet UITextField * secondTextField;
@property (nonatomic, retain) IBOutlet UITextField * doneButtonPressed;

@end

Add this to your .m file:

@synthesize firstTextField, secondTextField, doneButtonPressed;

- (BOOL)textFieldShouldReturn:(UITextField *)doneButtonPressed {
NSLog(@”Keyboard Done Pressed”);

[doneButtonPressed resignFirstResponder];

return YES;
}

Thanks to megatron.

Nov 152009

Here is a quick tip on how to reverse an array, from Simply-Hacking.org.

NSArray *reversedArray = [[array reverseObjectEnumerator] allObjects];
Nov 152009

Using Core Animation you can crossfade between two images.  The image on top is image1 and the bottom image is image2.

//This will fade image2 into image1 whenever it is called.
- (void) crossfade {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.5];
    imageView1.alpha = !imageView1.alpha;
    [UIView commitAnimations];
}
//  This will remove the 1st image and fade into the 2nd.
- (void) crossfade {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.5];
    [UIView setAnimationDelegate:imageView1];
    [UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];
    imageView1.alpha = 0
    [UIView commitAnimations];
}
Nov 152009

//  Here is the code, referenced from iphonedevelopertips site

- (void) loadImageInBackground
{
  NSURL *url = [NSURL URLWithString:
     @"http://iphonedevelopertips.com/images/logo-iphone-dev-tips.png"];

  // Assume image is an instance variable
  image = [[UIImage imageWithData: [NSData dataWithContentsOfURL:url]] retain];
}

...

[NSThread detachNewThreadSelector:@selector(loadImageInBackground)
   toTarget:self withObject:nil];

...

[self.view addSubview:[[UIImageView alloc] initWithImage:image]];

//  Here is the reference link
//  http://iPhoneDeveloperTips.com/cocoa/download-and-create-an-image-from-a-url.html