vofiella
vofiella

Reputation: 21

Objective C: UILabel not displaying the value that came from another class on runtime

I have a class here that passes a String value to another class which has a UILabel and display it to that. However i can't display it to the label of another class. I did this in 2 ways..

1)My First Way, directly calling the label from another class .On my first class, on the method

static void on_status_state(NSString *) stats
{
     DisplayViewController* display = [[DisplayViewController alloc] init];
        display.statusLabel.text = @"Sample Display";
}

On the class which contains the UILabel. On DisplayConn.h

@property (retain, nonatomic) IBOutlet UILabel *statusLabel;

2) My Second way, Calling a method and pass the value to a parameter on my first class, on the method

static void on_status_state(NSString *) stats
{
  DisplayViewController* display = [[DisplayViewController alloc] autorelease];
        [display toReceiveStatus:@"Sample Label"];
}

On the class which contains the UILabel. On DisplayConn.h

@property (retain, nonatomic) IBOutlet UILabel *statusLabel;

then on the DisplayConn.m

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)toReceiveStatus:(NSString *) stats
{
    self.statusLabel.text = stats;
    NSLog(@"DISPLAY %@",stats); 
}

The second way seems to be working because the log which contains the value is displayed, however the value is still not displayed to the label('statusLabel'). The display to label changes on runtime from time to time. What should be the cause?

Upvotes: 0

Views: 641

Answers (2)

Prasad G
Prasad G

Reputation: 6718

You should initialize the object of DisplayViewController class,

 static void on_status_state(NSString *) stats
{
        DisplayViewController* display = [[[DisplayViewController alloc]  init] autorelease];
        [display toReceiveStatus:@"Sample Label"];
}

You should check - (void)toReceiveStatus: is called or not. Declare statusLabel in DisplayConn.h as UILabel *statusLabel;

Try like this i think it will be helpful to you.

- (void)viewDidLoad
{
    statusLabel  = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
    statusLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    statusLabel.font = [UIFont systemFontOfSize:15.0];
    [self.view addSubview:statusLabel];

}

Upvotes: 0

Guru
Guru

Reputation: 5373

Consider this,

Class A and B

UILabel is in class B

in Class A,

B *newObject = [B alloc]init];
newObject.statusLabelValue = @"Simple Display";

and in your B.h

@property (retain, nonatomic) NSString *statusLabelValue;
@property (retain, nonatomic) IBOutlet UILabel *statusLabel;

and

in B.m

- (void)viewDidLoad
{
[super viewDidLoad];
statusLabel.text = statusLabelValue;
}

Upvotes: 1

Related Questions