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)