Saturday, June 2, 2012

Why we use #pragma mark in Objective-C/iPhone code and How to do that?

1. Why :  use pragma mark to help you organise your code for yourself  

Let! me make it more clear, When we work on huge projects and our classes contains tons of lines of code.
it's hard to manage all the methods specially when you are looking for any desired method and you need to scroll all the way again and again.

So #pragma is the way to organise the code, and provide you, Overview of the class showing all the methods, constants, properties etc.

Note: it doesn't affect anything on your code, just for the organising code

2. How : It's pretty simple when you start writing code just add #pragma above method or block every time when you need it.

Example :

#pragma mark - label name

so let's I have some life cycle methods of view controller then I will do like this

#pragma mark - ViewController's LIfe Cycle Methods

3. Here is Example code snip :

------------------------------------------------------------------------------------------------

#pragma mark - ViewController's LIfe Cycle Methods

- (void)viewDidLoad{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];    
}

#pragma mark - Button Click methods

- (IBAction)goButtonPressed:(id)sender {    
    readMeViewController = [[ReadMeViewController alloc] initWithNibName:@"ReadMeViewController" bundle:nil];
    readMeViewController.readMeControllerDelegate = self;
    [self.navigationController pushViewController:readMeViewController animated:YES];
    
}
------------------------------------------------------------------------------------------------
and whenever I need to look for my class overview just press ctrl+6 and below is the result

4. result (class overview) :



Cheers!! we will use it from now ..Happy Coding :)