【问题标题】:Passing data from one view controller to other view controller through xib file通过 xib 文件将数据从一个视图控制器传递到另一个视图控制器
【发布时间】:2013-06-13 00:31:02
【问题描述】:

我想做一个应用,它包括表单提交。在此我通过单视图应用程序创建一个新项目。在这个 firstViewController 中,我放置了按钮并按下它,它重定向到 secondViewController。

在这我有一个表格。并想从用户那里获取数据并通过提交按钮我想将其重定向到 thirdViewController 并希望在 thirdViewController 上打印表单数据。

在我写的 firstViewController.m 文件中

-(IBAction)nextVCButton:(id)sender
{

SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

NSLog(@"name : %@",self.nameTextField.text);
tempView.fullName = self.nameTextField.text;
[tempView setFullName:self.fullName];
[self.view addSubview:tempView.view];

}

在 SecondViewController.m 文件中

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

NSLog(@"received info %@", [self.fullName description]);

self.nameLabel.text = [self.fullName description];

}

在日志中我正在获取 (name :) 值,但在 secondViewController 中收到的信息为空。

我通过使用 xib 文件来完成这一切。不使用情节提要。

请帮我把这个表单数据传给 secondViewController。

谢谢。

【问题讨论】:

  • 使用 addsubview 加载第二个视图控制器?严重吗?那为什么要创建 UINavigationController 类?
  • 哪种方式更适合通过按钮将一个视图导航到另一个视图并再次从该控制器通过按钮导航到第三个控制器?

标签: iphone xib viewcontroller


【解决方案1】:

对不起,我不明白你为什么要写这两行代码:

tempView.fullName = self.nameTextField.text;
[tempView setFullName:self.fullName];

相反, 在第二视图控制器的.h file 中创建NSString 的属性

@property (nonatomic,retain)    NSString *strFullName;

并在.m file中合成。

@synthesize strFullName;

现在,

-(IBAction)nextVCButton:(id)sender
{

SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

NSLog(@"name : %@",self.nameTextField.text);
tempView.strFullName = self.nameTextField.text;
[self.view addSubview:tempView.view];
}

【讨论】:

  • 非常感谢。我已经做了同样的事情,但是因为这一行 [tempView setFullName:self.fullName];我越来越空了。现在删除它后它工作正常。我为此花了两天时间。感谢您的宝贵回答。
  • 欢迎您。快乐编码(:
  • 哪种方式更适合通过按钮将一个视图导航到另一个视图并再次从该控制器通过按钮导航到第三个控制器?
  • 可以根据您的应用需求。
  • 对于同样的问题,我两次都使用 addSubView 导航到另一个页面。通过按下第二个视图控制器中的按钮,它会给出“线程 1:EXC_BAD_ACCESS(code=1, address=0x69202020)”的错误
【解决方案2】:

如果你想从一个视图控制器导航到另一个视图控制器,我认为你需要使用Navigation controller。那么在这里你需要在ThirdViewController 中进行属性合成iVar“fullName”。并保留SecondViewController 中的值。

1. ThirdViewController.h

 @property(nonatomic, retain) NSString *fullName;

2。 ThirdViewController.m

@synthisize fullName;
dealloc {
[fullName release];
}

3. SecondViewController.m

-(IBAction)nextVCButton:(id)sender
{

SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

NSLog(@"name : %@",self.nameTextField.text);
tempView.fullName = self.nameTextField.text; 
[tempView setFullName:self.fullName];
[self.view addSubview:tempView.view];
}

【讨论】:

    【解决方案3】:

    尝试使用 presentViewController 方法而不是 addSubview....

    -(IBAction)nextVCButton:(id)sender
    {
        SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    
        NSLog(@"name : %@",self.nameTextField.text);
        tempView.strFullName = self.nameTextField.text;
        [self.view addSubview:tempView.view];
        [self presentViewController:tempView animated:YES completion:nil];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      相关资源
      最近更新 更多