0

我有临时变量eneity和两个 ViewController:OneViewControllerSecondViewController。它们相互跳转很好。现在用于eneity将值从OneViewController发送到SecondViewController。但是当 SecondViewController 返回值时eneity,OneViewController 无法再获得返回值。

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

感谢您的帮助;

添加:

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
4

1 回答 1

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变量。SecondViewControllerexamplobjviewDidLoadExample3ViewControllervc.examplobj = self

于 2013-05-03T09:28:26.463 回答