Pages

Wednesday, January 18, 2012

How to change of default title font in the UIActionSheet?

There is no direct way to access the UIActionSheet’s default title properties. There are couple of ways you can do this.
One is, to customize the UIActionSheet.
Second is, to add the label as the sub-view to the UIActionSheet, and set the default title to be nil. But if you do this, there are some other changes apart from adding the label as title. You need to set the frame of the buttons (of type ‘UIThirdPartButton’) inside the UIActionSheet. The code looks like something as follows:
UILabel *titleLabel =
[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 50.0)];
titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = k3PPTitle;
[actionSheet addSubview:titleLabel];
[titleLabel release];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
CGRect actionSheetRect = actionSheet.frame;
actionSheetRect.origin.y -= 40.0;
actionSheetRect.size.height = 300.0;
actionSheet.frame = actionSheetRect;
for (int counter = 0; counter < [[actionSheet subviews] count]; counter++) {
    UIView *object = [[actionSheet subviews] objectAtIndex:counter];
    if ([object class] == NSClassFromString(@”UIThreePartButton”)) {
        CGRect frame = object.frame;
        frame.origin.y = frame.origin.y + 30.0;
        object.frame = frame;
    }
}

How to implement single/double click on the UITableView?


  To get the touch/tap/move count on any control, there are certain methods in the UIResponder class.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event


To get the single/double click actions on UITableView, please follow the below steps:

1. Create a CustomTableViewCell, sub-class of the UITableViewCell. And override the above mentioned methods. 

#import ”CustomTableViewCell.h”
@implementation CustomTableViewCell
- (void)dealloc {
    [super dealloc];
}
- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // Use the same color and width as the default cell separator for now
    CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
    CGContextSetLineWidth(ctx, 0.5);
    for (int i = 0; i < [columns count]; i++) {
        CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue];
        CGContextMoveToPoint(ctx, f, 0);
        CGContextAddLineToPoint(ctx, f, self.bounds.size.height);
    }
    CGContextStrokePath(ctx);
    [super drawRect:rect];
}
- (void)addColumn:(CGFloat)position {
    [columns addObject:[NSNumber numberWithFloat:position]];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    NSLog(@”touchesBegan tapCount = %d”, [aTouch tapCount]);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    NSLog(@”touchesCancelled tapCount = %d”, [aTouch tapCount]);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    NSLog(@”touchesEnded tapCount = %d”, [aTouch tapCount]);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    NSLog(@”touchesMoved tapCount = %d”, [aTouch tapCount]);
}

2. In the UITableView’s cellForRowAtIndexPath method, add the 
CustomTableViewCell instead of UITableViewCell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[CustomTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
UILabel *regNoLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 3, 150, 15)];
//regNoLabel.text = [[self.resultArray objectAtIndex:indexPath.section] valueForKey:@”Ent_Regn_Nbr”];
regNoLabel.text = @”Apple”;
regNoLabel.font = [UIFont systemFontOfSize:12.0];
[cell addSubview:regNoLabel];
[regNoLabel release];
        return cell;


}
When you test the application with this, you can observe the tap count in the logs.

Automatic Reference Counting in IOS5


Automatic Reference Counting (ARC) for Objective-C makes memory management the job of the compiler. By enabling ARC with the new Apple LLVM compiler, you will never need to type retain or release again, dramatically simplifying the development process, while reducing crashes and memory leaks. The compiler has a complete understanding of your objects, and releases each object the instant it is no longer used, so apps run as fast as ever, with predictable, smooth performance.

How to download a large file with the iPhone SDK and avoid memory usage issues?


I was facing some problem while downloading the large content:
Using the NSURLConnection class to download a large file in an iPhone application, but it crashes every so often because it’s using too much memory. I’m doing the usual NSURLConnection usage, to append the received data to a NSMutableData object.
Solution to the above problem is:
In the delegate method connectionDidReceiveData, we can store the content in the file
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{     
 filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:save_name];     
 NSFileHandle *file1 = [NSFileHandle fileHandleForUpdatingAtPath: filename];     
 [file1 writeData: data];    
 [file1 closeFile]; 
}

6 steps for better designing of any application


  1. Structure
  2. Navigation
  3. Reduction
  4. Appearance
  5. Feedback
  6. Optimization
Structure to be defined by User Mental Model, the way a task is perceived by the end user.

Memory Management Tips!!!


  • Respond to Memory Warnings
  • Avoid Using Autoreleased Objects
  • Use Lazy Loading and Reuse
  • Avoid UIImage’s imageNamed:
  • Build Custom Table Cells and Reuse Them Properly
  • Override Setters Properly
  • Beware of Delegation
  • Use Instruments
  • Use a Static Analysis Tool
  • Use NSZombieEnabled