Where to put declaration in Objective C
I have a file1.h file1.m file1.xib the xib has a blank screen with a label on it. I have another file 开发者_如何学Pythonthat does some arbitrary calculation and will update the label as the calculation loops. Where should I declare the UILabel * value in file1.h or the file that does the calculations?
Thank you drive around.
The label should be declared as an IBOutlet as Josh said in your .h file as above and yes connect your label in Interface Builder.
You can also define your label as @property in .h file and synthesize it in .m file so that you can easily access "myLabel" using . operator.
Now to update your label with your calculations, simply define the updateLabel function in .h file and write your code to implement update in the implementation file as below:
@interface File1 {
IBOutlet UILabel *myLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
- (void)updateLabel:(id)sender;
@end
@implementation File1
@synthesize myLabel;
- (id)init {
if ( (self = [super init]) ) {
// init custom work here
}
return self;
}
- (void)updateLabel:(id)sender {
//Here sender can be any button who call this function or send it nil if not
//update your label here
myLabel.text = @"Updated Text";
......
}
@end
The label should be declared as an IBOutlet
in your .h file:
@interface File1 {
IBOutlet UILabel *mylabel;
}
@end
Make sure you connect this outlet to your label in Interface Builder.
精彩评论