2 square10 square10 于 2013.05.03 12:18 提问

利用临时变量在两个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就贴了

2个回答

zhanglei5415
zhanglei5415   Rxr 2013.05.06 09:06
已采纳

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

你在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的引用.
    最好的方案还是使用代理.优势是解藕,不那么紧依赖

MrsSeven7
MrsSeven7   2013.05.03 14:43

首先添加object到Application Delegate。

然后创建它的属性property。

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

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

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

square10
square10 首先添加object到Application Delegate。你的意思是,在我的 AppDelegate.h里,添加我自己的test.h,然后property它:test.那AppDelegate *objAppDelegae在哪个文件里实例化它呢?
4 年多之前 回复
MrsSeven7
MrsSeven7 再看一下,哪步不懂呢?
4 年多之前 回复
square10
square10 感谢,但才转IOS没好久,不是很清楚你的意思,我代码贴出来了,不知道有没有更详细的说明呢?
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!