臨時變量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的引用.
最好的方案還是使用代理.優勢是解藕,不那麼緊依賴