Hi,
I have some questions about "@property" and "@synthesize". Below is two sample codes.
Sample code 1 (with synthesize IBOutlet and instance variable).
ChangeText.h
@interface ChangeText : UIViewController {
UIButton * btn;
IBOutlet UITextField * txtField;
}
@property(nonatomic, retain) UIButton * btn;
@property(nonatomic, retain) IBOutlet UITextField * txtField;
-(IBAction)changeTxt : (id)sender;
@end
ChangeText.m
@implementation ChangeText
@synthesize btn;
@synthesize txtField;
-(void)changeTxt : (id)sender
{
txtField.text = @"Testing";
}
@end
But when I removed the @property and @synthesize. It still work fine as sample code 1. So, in my case, is it really a need to use @property and @synthesize ? What is the differences between add and remove @property and @synthesize in our code ? What is the effects of add @property and @synthesize to our code ? Below is the code with removed @property and @synthesize.
Sample code 2 (without synthesize IBOutlet and instance variable).
ChangeText.h
@interface ChangeText : UIViewController {
UIButton * btn;
IBOutlet UITextField * txtField;
}
-(IBAction)changeTxt : (id)sender;
@end
ChangeText.m
@implementation ChangeText
-(void)changeTxt : (id)sender
{
txtField.text = @"Testing";
}
@end
Hope someone can reply me. Thanks for your help.
I have some questions about "@property" and "@synthesize". Below is two sample codes.
Sample code 1 (with synthesize IBOutlet and instance variable).
ChangeText.h
@interface ChangeText : UIViewController {
UIButton * btn;
IBOutlet UITextField * txtField;
}
@property(nonatomic, retain) UIButton * btn;
@property(nonatomic, retain) IBOutlet UITextField * txtField;
-(IBAction)changeTxt : (id)sender;
@end
ChangeText.m
@implementation ChangeText
@synthesize btn;
@synthesize txtField;
-(void)changeTxt : (id)sender
{
txtField.text = @"Testing";
}
@end
But when I removed the @property and @synthesize. It still work fine as sample code 1. So, in my case, is it really a need to use @property and @synthesize ? What is the differences between add and remove @property and @synthesize in our code ? What is the effects of add @property and @synthesize to our code ? Below is the code with removed @property and @synthesize.
Sample code 2 (without synthesize IBOutlet and instance variable).
ChangeText.h
@interface ChangeText : UIViewController {
UIButton * btn;
IBOutlet UITextField * txtField;
}
-(IBAction)changeTxt : (id)sender;
@end
ChangeText.m
@implementation ChangeText
-(void)changeTxt : (id)sender
{
txtField.text = @"Testing";
}
@end
Hope someone can reply me. Thanks for your help.