2

I have got two ViewController:

1) ViewController

2) TestAppViewController

In ViewController.h, I define a label, whose text I want to be sent from second viewController, i.e `TestAppViewController.

For this I defined a @property NSString * in ViewController.h, and created an object of this ViewController in my second Controller. Then passed a value to this property, but ans is still coming nil.

Following is my code :

@property (nonatomic, retain) NSString *lableName;*

ViewController

#import "ViewController.h
#import "TestAppView1.h"
#import "TestAppViewController2.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //    [self addView1];
}

- (void)viewWillAppear:(BOOL)animated
{ 
    [self addView1];

    NSLog(@"Label name is %@",self.lableName);
}    

- (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

- (void)addView1
{
    TestAppView1 *testAppView1Obj = [ [TestAppView1 alloc]        initWithFrame:self.view.bounds];

    [testAppView1Obj setDelegate:self];
    [testAppView1Obj setSelectorOnButtonTapped:@selector(buttonTapped)];

    [self.view addSubview:testAppView1Obj];
}

 -(void)buttonTapped
 {

    TestAppViewController2 *testAppViewController2Obj =[[TestAppViewController2    alloc]initWithNibName:@"TestAppViewController2" bundle:nil];

    [self.navigationController pushViewController:testAppViewController2Obj animated:YES];
 }

 @end

ViewController2

#import "TestAppViewController2.h"
#import "TestAppView2.h"
#import "ViewController.h"

@interface TestAppViewController2 ()

@end

@implementation TestAppViewController2

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [self addView2];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addView2
{
    TestAppView2 *testAppView2Obj =[ [TestAppView2 alloc]initWithFrame:self.view.bounds];

    [testAppView2Obj setDelegate:self];
    [testAppView2Obj setSelectorOnBackButton:@selector(callbackButtonTapped)];
    [self.view addSubview:testAppView2Obj];
}

-(void)callbackButtonTapped
{

    ViewController *viewControllerObj = [[ViewController alloc]init];

     viewControllerObj.lableName=@"Yogesh";    
     [self.navigationController popViewControllerAnimated:YES];


}

@end

When I try to print the values in NSLog, it gives me nil.

Irfan
  • 4,301
  • 6
  • 29
  • 46
yogesh
  • 79
  • 2
  • 2
  • 6
  • 1
    http://stackoverflow.com/questions/15401852/how-to-pass-data-from-one-view-to-another-view-in-iphone – iPatel Sep 02 '13 at 05:47
  • I suggest using (strong, nonatomic) if you using arc as well – DogCoffee Sep 02 '13 at 05:50
  • pass through init or use delegate – Arun Sep 02 '13 at 05:53
  • Make sure that you have implemented it properly.You are allocating a new view controller and pop from stack.The code is not cleared.In your callbackbuttontapped what is the use of allocating that view as your are not using this. Instead of pop push the view to see the effects. – kumar123 Sep 02 '13 at 05:57
  • @spynet..can you help me by showing an example of using delegate.. to send values between controllers – yogesh Sep 02 '13 at 06:06
  • @iPhoneDev..i am tryin to send data using popViewController... for that i have to work with delegates.. please give me detailed info on that – yogesh Sep 02 '13 at 06:07
  • This works perfectly use the code from the link http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers/20048208#20048208 – user2998756 Nov 18 '13 at 12:53

1 Answers1

6

Using delegate:

In TestAppViewController.h:

   @protocol messageDelegate <NSObject>
    @optional
    -(void)test:(NSString*)str;
    @end
    @interface SecondViewController : NSString
    @property (nonatomic, assign) id <messageDelegate> delegate;
    @end

In TestAppViewController.m:

-(void)readyToSend
{
    [self.delegate test:@"Hello world!"];

}

In ViewController.h:

@interface ViewController : UIViewController<messageDelegate>
@end

In ViewController.m: in

- (void)viewDidLoad {
TestAppViewController * testAppViewController = [[TestAppViewController alloc] init];
testAppViewController.delegate = self;
}
-(void) test:(NSString*)str{
NSLog(@"%@,str");
}

Hope it will help!

user2545330
  • 408
  • 4
  • 17