I am new to iphone development and just in learning phase. I am learning from books and Video lectures while I saw this code Which I am unable to understand
- (IBAction)logoff:(id)sender {
//some code here
}
here I do not understand that is id a data type or some entity. and what could be the reason to pass id as argument.
and at anohter place I saw
if(self)
{
// some code
}
I do not understand why he pass self in if. what the reason to check self. Should we check self before any time we use it.
Please it would be more helpful for me if you tell the reason that why he use this so that I could you it in my codes efficiently and reasonably.
thanks
Lots of controls (UIButton, UISwitch, UIBarButtonItem) can be connected to the same action method. Because the sender is of type
idit will accept lots of different sender types, i.e. the sender type isn’t restricted to only a UIButton.Maulik’s remark that the argument represents the tag is wrong, it represents the object (e.g. a UIControl) that sent the message. The object can be typecasted though in order to retrieve the tag, provided the type to which the sender is casted to contains the tag property and the sender is of the correct type.
Now about your other question:
selfis checked to be not nil before proceeding. Sometimes initialization can fail for several reasons (e.g. memory issues). If the object failed to initialize properly there’s not much you can do with it (no access to ivars for example, since no memory was allocated for the ivars).