是否可以使用临时变量传递值

时间:2013-05-03 06:02:30

标签: iphone objective-c

我有临时变量eneity和两个ViewController: OneViewController SecondViewController 。他们互相跳转。现在使用eneity发送从 OneViewController SecondViewController 的值。但是当SecondViewController返回值为eneity时,OneViewController无法再获取返回值。

我想知道我是否可以使用变量来获取返回值,而不是委托?

感谢您的帮助;

ADD:

firstViewController,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.
//when IBOutlet not be used,use program to makd connection
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

secondeViewController,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

1 个答案:

答案 0 :(得分:0)

你不能写

Example3ViewController* e3v = [[Example3ViewController alloc] initWithNibName:@"Example3ViewController" bundle:[NSBundle mainBundle]];
e3v.b = t;

这将创建Example3ViewController类的另一个对象,并将t值分配给该对象,而不是分配给堆栈中已有的对象。

你可以像

一样写
Example3ViewController* e3v = [((AppDelegate *)[AppDelegate sharedInstance]).navigationController.viewControllers objectAtIndex:[((AppDelegate *)[AppDelegate sharedInstance]).navigationController.viewControllers count]-1];

您可以在Example3ViewController(例如SecondViewController)和examplobj viewDidLoad中分配Example3ViewController变量vc.examplobj = self