Flipkart Search

Search This Blog

Friday, October 8, 2010

UITableView backgroundColor always gray on iPad (fix)

Here is a quick fix to get rid of gray background color on grouped style UITableView.

if ([tableView respondsToSelector:@selector(backgroundView)]){
            tableView.backgroundView = nil;
        [tableView setBackgroundColor:[UIColor clearColor]];
}

Friday, September 24, 2010

Reverse objects of an NSArray/NSMutableArray easily

Here is an example of reversing a NSArray/NSMutableArray

    NSString *string = @"a,b,c,d,e,f,g,h,i,j,k";
    NSArray *array = [string componentsSeparatedByString:@","];
    NSLog(@"array = %@",array);
    NSArray* reversedArray = [[array reverseObjectEnumerator] allObjects];
    NSLog(@"Reverse array = %@",reversedArray);



Happy coding all iPhone coderz

Thursday, July 29, 2010

Put double quotes in NSString

Here is a simple way to put  double quotes in your NSString

NSString *myString = @"I say \"Hello!\"";
The output will be
I say "Hello!"

Thursday, July 8, 2010

Generate Random Color

Here is a simple method to generate a random color :-
just put this method in any class you want and put the definition in the header file

in .h file
+ (UIColor *) randomColor;

in .m file
+ (UIColor *) randomColor {
    CGFloat red =  (CGFloat)random()/(CGFloat)RAND_MAX;
    CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
    CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

get random color by calling this method like this....

//Class Name is the class where you have put the method
UIColor *randColor = [ClassName randomColor];

UIKeyboardBoundsUserInfoKey is deprecated, what to use instead?

From the documentation for UIKeyboardBoundsUserInfoKey:
The key for an NSValue object containing a CGRect that identifies the bounds rectangle of the keyboard in window coordinates. This value is sufficient for obtaining the size of the keyboard. If you want to get the origin of the keyboard on the screen (before or after animation) use the values obtained from the user info dictionary through the UIKeyboardCenterBeginUserInfoKey or UIKeyboardCenterEndUserInfoKey constants. Use the UIKeyboardFrameBeginUserInfoKey or UIKeyboardFrameEndUserInfoKey key instead.
Apple recommends implementing a convenience routine such as this (which could be implemented as a category addition to UIScreen):
+ (CGRect) convertRect:(CGRect)rect toView:(UIView *)view {
    UIWindow *window = [view isKindOfClass:[UIWindow class]] ? (UIWindow *) view : [view window];
    return [view convertRect:[window convertRect:rect fromWindow:nil] fromView:nil];
}
 
to recover 
window-adjusted keyboard frame size properties.I took a different approach, which involves checking the device 
orientation: 
CGRect _keyboardEndFrame;
[[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardEndFrame];
CGFloat _keyboardHeight;
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
    _keyboardHeight = _keyboardEndFrame.size.height;
}
else {
    _keyboardHeight = _keyboardEndFrame.size.width;
}
 
Thanks to Alex Reynolds for the answer
 

Tuesday, July 6, 2010

Opting Out of Background Execution in iPhone OS 4.0

If you do not want your application to remain in the background when it is quit, you can explicitly opt out of the background execution model by adding the UIApplicationExitsOnSuspend key to your application’s Info.plist file and setting its value to YES. When an application opts out, it cycles between the not running, inactive, and active states and never enters the background or suspended states. When the user taps the Home button to quit the application, the applicationWillTerminate: method of the application delegate is called and the application has approximately five seconds to clean up and exit before it is terminated and moved back to the not running state.
Opting out of background execution may be preferable for certain types of applications. Specifically, if coding for the background may require adding significant complexity to your application, terminating the application may be a simpler solution. Also, if your application consumes a large amount of memory, the system might need to terminate your application quickly anyway to make room for other applications. Thus, opting to terminate, instead of switch to the background, might yield the same results and save you development time and effort.

Tuesday, June 22, 2010

Customize the default UISlider in iPhone

here is a simple way to customize the UISlider images

UIImage *stetchLeftTrack = [[UIImage imageNamed:@"left-slide.png"]
                                stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
    UIImage *stetchRightTrack = [[UIImage imageNamed:@"right-slide.png"]
                                 stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
    [sldDistanceFilter setThumbImage: [UIImage imageNamed:@"slider_ball.png"] forState:UIControlStateNormal];
    [sldDistanceFilter setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
    [sldDistanceFilter setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];

You just need to replace the image names according to your suite.

Wednesday, May 12, 2010

Limit Characters in UITextField

Here is a simple delegate method to limit the characters in UITextfield :-

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
        NSUInteger newLength = [textField.text length] + [string length] - range.length;
        return (newLength > 8) ? NO : YES;
}

Saturday, April 17, 2010

Difference b/w NSArray and NSMutableArray

NSMutableArray (and all other classes with Mutable
in the name) can be modified. So, if you create a plain NSArray,
you cannot change its contents later (without recreating it). But if
you create an NSMutableArray, you can change it.

Friday, April 16, 2010

Difference between signed and unsigend data types

Signed Data Type - a signed data type is one that can hold both positive or negative values, i.e. -1 or +456. A 32 bit signed integer can hold the range -2147483648 to 2147483647

Unsigned Data Type - Unsigned data types can only hold positive values.
An unsigned 32 bit integer for instance can hold the range 0 to 4294967296