将数据从一个ViewController发送到另一个ViewController。

时间:2013-09-02 05:39:47

标签: ios iphone objective-c callback viewcontroller

我有两个ViewController:

1) ViewController

2) TestAppViewController

ViewController.h中,我定义了一个标签,我希望从第二个viewController发送其文本,即`TestAppViewController。

为此我在 ViewController.h 中定义了一个@property NSString *,并在我的第二个Controller中创建了一个ViewController的对象。 然后将值传递给此属性,但是ans仍然是零。

以下是我的代码:

@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

当我尝试在NSLog中打印值时,它给我零。

2 个答案:

答案 0 :(得分:6)

使用委托:

在TestAppViewController.h中:

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

在TestAppViewController.m中:

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

}

在ViewController.h中:

@interface ViewController : UIViewController<messageDelegate>
@end

在ViewController.m中:

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

希望它会有所帮助!

答案 1 :(得分:0)

这是将数据从一个控制器传递到其他

的简单解决方案

检查此处的代码Passing Data between View Controllers