square10 2013-05-03 04:18 采纳率: 100%
浏览 2282
已采纳

利用临时变量在两个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条回答 默认 最新

  • ReyZhang 移动开发领域新星创作者 2013-05-06 01: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的引用.
      最好的方案还是使用代理.优势是解藕,不那么紧依赖

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题