Saturday, February 25, 2012

Add effect to the rounded button; create the customed button

If you want to see the color change on the button in the normal stage and button-pressed stage, you may add the below code in the viewDidLoad.



 //create the button to clear Screen
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
    
    //set the position of the button
    button.frame = CGRectMake(0, 0, 150, 40);
    button.center = CGPointMake(160, 240);
    
    //set the button's title
    [button setTitle:@"Clear!" forState:UIControlStateNormal];
    
    //set button effect
    UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
    UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [button setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
    
    UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"];
    UIImage *stretchableButtonImagePressed = [buttonImagePressed   stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [button setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
    
    //add the button to the view
    [self.view addSubview:button];


 whiteButton.png

 blueButton.png
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


The following code create the customed button with own image.



UIImage *buttonImage = [UIImage imageNamed:@"cancel.png"];

    UIbutton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(280.0, 10.0, 29.0, 29.0);
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clearScreen:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];


Friday, February 24, 2012

Add border to the imageView and textView

Add the border and maker the rounded corner to the imageView and textView:


in .h file
         #import <QuartzCore/QuartzCore.h>
        

in .m file
        // adding border to the imageView
        [theImageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
        [theImageView.layer setBorderWidth: 1.0];

         theImageView.layer.cornerRadius = 9.0;
         theImageView.layer.masksToBounds = YES;
        
        
        // resize the imageView to fit the image size
        CGSize size = [image size];
        float factor = size.width / self.frame.size.width;
        if (factor < size.height / self.frame.size.height) {
            factor = size.height / self.frame.size.height;
        }
        
        CGRect rect = CGRectMake(0, 0, size.width/factor, size.height/factor);
        theImageView.frame = rect;

Thursday, February 23, 2012


Even you had valid icon in PNG.format along with its size (57x57), when you upload the binary to iTunes Connect, you may receive the following error message.


iPhone/iPod Touch: XXX.png: icon dimensions (0 x 0) don't meet the size requirements. The icon file must be 57x57 pixels, in .png format.


The solution is as below for Xcode 4.2.


Edit Project -> Build setting -> Packaging -> set 'NO' to Compress PNG Files

MAC - access folders that contain space in Terminal, e.g. My Folder

1. go to particular directory, cd "My Folder" or

2. at the root directory, cd + space, then drag the folder icon from the Finder into the Terminal window.

Friday, February 17, 2012

Core Data: could not locate an NSManagedObjectModel for entity name 'XXX'

The following code is a simple trick to solve this issue. Add it to viewDidLoad or somewhere the code to access the entity.



if (managedObjectContext == nil)   { 
        managedObjectContext = [(YourAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; }

Remember to import "YourAppDelegate.h" !!!

Sunday, February 12, 2012

Create time stamp

[NSDate date] can return the time which refers to GMT.
If you want the appropriate time zone, you need to plus/minus the time different with [NSDate date].
The below code can make up the time for Hong Kong time zone.



- (NSDate *)timeStamp
{
    // Get GMT time
    NSTimeInterval rightNowInterval = [[NSDate date] timeIntervalSince1970];
   
    // time difference is 8 hours between GMT and Hong Kong
    NSTimeInterval estoniaTimeZoneDifference = 60*60*8;//60 seconds, 60 minutes, 8 hours

    // Hong Kong is 8 hours ahead GMT
    NSDate *timeStamp = [NSDate dateWithTimeIntervalSince1970:(rightNowInterval + estoniaTimeZoneDifference)];
    
    return timeStamp;


}

Saturday, February 11, 2012

Explore the files in the directory; determine if the file exists in directory




//explore the files in directory
- (void)viewDidLoad
{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSLog(@"Documents directory: %@", [fileManager contentsOfDirectoryAtPath:documentDirectory error:nil]);
}

// determine the file exists in the directory

-(NSString *)dataFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    return [documentDirectory stringByAppendingPathComponent:data.plist];
}

- (void)viewDidLoad
{

        NSString *filePath = [self dataFilePath];
    
        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
        {
            NSMutableArray *database = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
            
            other code...

        }

        [super viewDidLoad];
}


Dynamically change the size of UITableView in iPad DetailViewController

If you add the UITableView to the UIView in iPad's DetailViewController,  the following code can change the dimension of UITableView based on interfaceOrientation.

in .h file

@interface DetailViewController : UIViewController < UITableViewDataSource, UITableViewDelegate, UIPopoverControllerDelegate, UISplitViewControllerDelegate>
{
    UITableView *myTableView;

}

in .m file

- (void)viewDidLoad
{


CGRect frame = [UIScreen mainScreen].applicationFrame;
    myTableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    myTableView.delegate= self;
    myTableView.dataSource = self;
    [self.view addSubview:myTableView];

[super viewDidLoad];

}





- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown
    {
        
        // Portrait
        CGRect frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height);
        myTableView.frame = frame;
        
    }
    else {
        
        // Landscape

        CGRect frame = CGRectMake(0, 0, 703, self.view.frame.size.height);
        myTableView.frame = frame;
       
    }
     
    return YES;
}

Obtain the size of application screen

Obtain the size of application screen:

CGRect frame = [UIScreen mainScreen].applicationFrame; Or

CGRect frame = [UIScreen mainScreen] bounds];

Obtain the width of application screen (Same way for height):

UIWindow* window = [UIApplication sharedApplication].keyWindow;
float width = window.frame.size.width;
Or, float width = [UIScreen mainScreen] bounds].size.width;

Friday, February 10, 2012

UITableView: select the row to display the popover view

The following code will help you to pop up the popover view when I select the row in UITableView.



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [myTableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.newPopover == nil
    {
    
        TasksViewController *tasksViewController = [[TasksViewController alloc] init];        
        tasksViewController.navigationItem.title = @"Whatever you like";
        UINavigationController *navController =  [[UINavigationController alloc] initWithRootViewController:tasksViewController];
        UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navController]; 
        
        popover.delegate = self;
        [tasksViewController release];
        [navController release];
        
        self.newPopover = popover;
        [popover release];
    }
     newPopover.popoverContentSize = CGSizeMake(320, 360);
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     [newPopover presentPopoverFromRect:cell.bounds inView:cell.contentView permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
    
     
    
}

Tuesday, February 7, 2012

MAC - cut & paste; copy & paste

Cut and paste can be done in Lion.

1. click the file at the original folder, then cmd+c;
2. go to destination, then option +cmd+v (without option, it performs copy & paste)