Methods in Objective c


 

Methods in Objective c

If the method is declared with "+" sign suggest that it's a class method where as method that begins with the "-" sign makes it a instance method. The method declaration in objective c also take arguments and return types.

If the method is declared with "+" sign suggest that it's a class method where as method that begins with the "-" sign makes it a instance method. The method declaration in objective c also take arguments and return types.

Methods in Objective c

The example discuss about method in objective c. In objective c programming language method can be declared using either "+" or "-" sign.

If the method is declared with "+" sign suggest that it's a class method where as method that begins with the "-" sign makes it a instance method. The method declaration in objective c also take arguments and return types. Argument can be declared after " : " sign.

In Objective c, we writes the codes in two different files " interface" and "implementation" and the extension for these files are .h and .m respectively.

In the header file (.h) we generally, declares the variables, methods and superclass name where as an implementation files holds the operation codes of those methods.

To write a method in objective c .. just follow the given steps:
1. Declare the method in header(.h) file.
2. and implement it into the .m file

a simple example:

Header File(.h)

#import <UIKit/UIKit.h>

@interface methodViewController : UIViewController {

NSString *string;

UIButton* button;
UILabel* label;
}

@property(nonatomic,retain)IBOutlet UIButton* button;
@property(nonatomic,retain)IBOutlet UILabel* label;

-(void)print:(id)sender;

@end

Implementation file (.m)
#import "methodViewController.h"

@implementation methodViewController
@synthesize button;
@synthesize label;

- (void)dealloc {

[super dealloc];
[button release];
[label release];
}

-(void)print:(id)sender {

string= @"method declaration in Objective C";
label.text =string;

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

@end

The output is:

Download Code

Ads