Enviando datos de un ViewController a otro.
Tengo dos ViewController:
1)ViewController
2)TestAppViewController
EnViewController.h
, Defino una etiqueta, cuyo texto quiero enviar desde el segundo viewController, es decir, `TestAppViewController.
Para esto definí una@property NSString *
enViewController.h, y creé un objeto de este ViewController en mi segundo Controlador. Luego se le pasa un valor a esta propiedad, pero ans sigue llegando a cero.
Lo siguiente es mi código:
@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
Cuando intento imprimir los valores en NSLog, me da nulo.