0

I'm a beginner iPhone developer.

I have 2 classes. TestViewController (this is linked withe view in the storyboard) and ViewController.

I am trying to call a method([tc refresh:val];) in TestViewController with val as the parameter from ViewController class. I can see from the logs that val reaches the TestViewController, but for some reason, the label does not update and I do not get the current text of the label also which I have set in the view. It gives null value. Please see the code and log i get and advice me how can I get the label updated by calling a method from VC to TVC.

TestViewController.h

#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface TestViewController : UIViewController

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

- (IBAction)chngText:(id)sender;
- (void)refresh:(NSString *)val;

@end

TestViewController.m

#import "TestViewController.h"
#import "ViewController.h"

@implementation TestViewController
@synthesize lblDisp;
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"TEST VC LOADED");
    NSLog(@"TEXT CUrret VALUE SUPERVIEW %@",lblDisp.text);
}

- (IBAction)chngText:(id)sender {
    ViewController *dd=[[ViewController alloc]init];
    [dd display];
}

-(void)refresh:(NSString *)val{
    NSLog(@"Value of Val = %@",val);
    NSLog(@"TEXT CUrret VALUE %@",lblDisp.text);
    lblDisp.text=val;
}
@end

ViewController.h

#import <UIKit/UIKit.h>
#import "TestViewController.h"

@interface ViewController : UIViewController

-(void)display;

@end

ViewController.m

#import "ViewController.h"
#import "TestViewController.h"

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"VC LOADED");
}

-(void)display{
    NSLog(@"Reached VC");
   NSString *val=@"1";
    TestViewController *tc=[[TestViewController alloc]init];
    [tc refresh:val];
}

@end

Log

2013-07-24 03:13:36.413 Simple test[38477:11303] TEST VC LOADED
2013-07-24 03:13:36.415 Simple test[38477:11303] TEXT CUrret VALUE SUPERVIEW sdfgdfgd
2013-07-24 03:13:37.909 Simple test[38477:11303] Reached VC
2013-07-24 03:13:37.910 Simple test[38477:11303] Value of Val = 1
2013-07-24 03:13:37.911 Simple test[38477:11303] TEXT CUrret VALUE (null)
Can
  • 8,502
  • 48
  • 57
Reuben Kurian
  • 123
  • 1
  • 2
  • 12

2 Answers2

4

Your problem lies in the way you pass the value back.

TestViewController *tc=[[TestViewController alloc]init];
[tc refresh:val];

The first line creates and initializes a new instance tc of your TestViewController Class. This allows you to access its methods, but that doesn't mean you're accessing the instance you created initially or the data you originally assigned. This means your label lblDisp, along with the rest of your new TestViewController instance's properties, are nil.

Basically you can't use this strategy to pass data back and forth. See this SO post:

Passing Data between View Controllers

Community
  • 1
  • 1
Chris Tetreault
  • 1,973
  • 13
  • 19
2

The problem is pretty simple

  -(void)refresh:(NSString *)val{
        NSLog(@"Value of Val = %@",val);
        NSLog(@"TEXT CUrret VALUE %@",lblDisp.text);
        lblDisp.text=val;
    }

here after the log is printed, value is updated so change the code like this order in which label is updated first and then log the value

So use

  -(void)refresh:(NSString *)val{
        lblDisp.text=val;
        NSLog(@"Value of Val = %@",val);
        NSLog(@"TEXT CUrret VALUE %@",lblDisp.text);
    }

EDIT

Well the reason may be the lblDisp is loaded from nib and it is not a valid memory till the view is presented/pushed .And then only the method will have valid label instance and then only you can update it

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101