views:

49

answers:

2

I have a file1.h file1.m file1.xib the xib has a blank screen with a label on it. I have another file that 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.

A: 

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.

Josh Hinman
It probably wouldn't hurt to also review the memory management guidelines for NIB objects (https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmNibObjects.html#//apple_ref/doc/uid/TP40004998-SW2) to make sure your view controller behaves properly in low memory situations.
Robot K
A: 

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

mysticboy59