利用临时变量在两个ViewController之间传值

临时变量eneity,如有两个ViewController,OneViewController,SecondViewController两个ViewController,跳转正常,从OneViewController利用临时变量eneity发送值到SecondViewController,但是SecondViewController和回传值,利用eneity,OneViewController却得不到,在不用代理的情况下,是否可以利用变量,得到回传值。


补充
现在把代码贴出来:
第一个ViewController,Example3ViewController:

#import "Example3ViewController.h"

@interface Example3ViewController ()

@end

@implementation Example3ViewController
@synthesize editorVC;
@synthesize labelname;
@synthesize labelnumber;
@synthesize labelsummary;
@synthesize b;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //当不使用IBOutlet时,需要用程序来构造连接
    EditorViewController* vc = [[EditorViewController alloc]initWithNibName:@"EditorViewController" bundle:nil];
    self.editorVC = vc;
    vc = nil;
}

- (void)viewWillAppear:(BOOL)animated{
    NSLog(@"%@",self.b.name);
    self.labelname.text = self.b.name;
    self.labelnumber.text = self.b.number;
    self.labelsummary.text = self.b.summary;
}

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

- (IBAction)edit:(id)sender{
    [self presentModalViewController:editorVC animated:YES];
}

@end

第二个ViewController,EditorViewController:

#import "EditorViewController.h"
#import "Example3ViewController.h"
#import "test.h"

@interface EditorViewController ()

@end

@implementation EditorViewController
@synthesize vtitle;
@synthesize number;
@synthesize summary;

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

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

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

- (IBAction)done:(id)sender{
    //[[self parentViewController]dismissModalViewControllerAnimated:YES];
    //Example3ViewController* e3v = [[Example3ViewController alloc]init];]

    //e3v.name = title.text;
    //e3v.number = number.text;
    //e3v.summary = summary.text;
    //[self dismissViewControllerAnimated:YES completion:^(void){

    //}];
    test* t = [[test alloc]init];
    t.name = self.vtitle.text;
    t.number = self.number.text;
    t.summary = self.summary.text;
    //[t setName:vtitle.text];
    //[t setNumber:number.text];
    //[t setSummary:summary.text];
    Example3ViewController* e3v = [[Example3ViewController alloc] initWithNibName:@"Example3ViewController" bundle:[NSBundle mainBundle]];
    e3v.b = t;

    [self dismissModalViewControllerAnimated:YES];
    [t release];
    [e3v release];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [vtitle resignFirstResponder];
    [number resignFirstResponder];
    [summary resignFirstResponder];
}
@end

临时变量:

#import "test.h"

@implementation test
@synthesize name;
@synthesize number;
@synthesize summary;
@end

m就贴了

你知道问题的根本出在哪里吗?

你在EditorViewController 中的

  • (IBAction)done:(id)sender
    创建了test对象实例t,并将t实例传给了Example3ViewController类型的e3v 对象的b 属性. 接下来你并没有显示Example3ViewController的view
    而只是实例化了一个e3v对象,我们知道通过Nib来创建控制器实例,只会调用控制器的

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 方法
    只有当控制器的视图将要被呈现到屏幕中去的时候才会调用控制器的

  • (void)viewWillAppear:(BOOL)animated 方法
    所以Example3ViewController中的viewWillAppear 方法根本就没有被执行,故此你没有看到视图上的uilabel被赋值.

    其根本原因还是出在了,你认为Example3ViewController 在内存中是一个持久对象

    解决方案:

    解决的方法不只是一种,你可以在EditorViewController 中添加一个对Example3ViewController的引用.
    最好的方案还是使用代理.优势是解藕,不那么紧依赖

首先添加object到Application Delegate。

然后创建它的属性property。

然后用appDelegate 的object 访问object,并且随时初始化,像这样:

AppDelegate *objAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//像这样进行初始化
objAppDelegate.yourObject = some value;
// 或者像这样提供值
id newObject =  objAppDelegate.yourObject

在应用中的任何一个控制器中可使用这个object。