将数据从一个ViewController发送到另一个。 [英] Sending data from one ViewController to another.
问题描述
我有两个ViewController:
I have got two ViewController:
1) ViewController
2) TestAppViewController
在 ViewController.h
一个标签,它的文本我想从第二个viewController发送,即`TestAppViewController。
In ViewController.h
, I define a label, whose text I want to be sent from second viewController, i.e `TestAppViewController.
为此我定义了一个 @property NSString * code>在 ViewController.h ,并在我的第二个控制器中创建了这个ViewController的对象。
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.
以下是我的代码:
@property (nonatomic, retain) NSString *lableName;*
$ b b
ViewController
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
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中的值,它给我nil。
When I try to print the values in NSLog, it gives me nil.
推荐答案
使用委托:
在TestAppViewController.h中:
In TestAppViewController.h:
@protocol messageDelegate <NSObject>
@optional
-(void)test:(NSString*)str;
@end
@interface SecondViewController : NSString
@property (nonatomic, assign) id <messageDelegate> delegate;
@end
在TestAppViewController.m中:
In TestAppViewController.m:
-(void)readyToSend
{
[self.delegate test:@"Hello world!"];
}
在ViewController.h中:
In ViewController.h:
@interface ViewController : UIViewController<messageDelegate>
@end
在ViewController.m中:
In ViewController.m: in
- (void)viewDidLoad {
TestAppViewController * testAppViewController = [[TestAppViewController alloc] init];
testAppViewController.delegate = self;
}
-(void) test:(NSString*)str{
NSLog(@"%@,str");
}
希望它会有帮助!
这篇关于将数据从一个ViewController发送到另一个。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!