【问题标题】:Passing textfield data between views在视图之间传递文本字段数据
【发布时间】:2014-05-02 15:23:23
【问题描述】:

我创建了两个视图,每个视图都有四个文本字段。我有一个开关,可以让我在这两个视图之间来回切换。当开关被激活/停用时,它将当前文本字段数据传递给目标 ViewController,反之亦然。

以下代码最初运行良好。如果我在三个文本字段中输入数据,比如在 FirstViewController 中,按下开关,数据会出现在 SecondViewController 对应的三个文本字段中。如果我再次按下开关,来自相同三个文本字段的数据将填充 FirstViewController 中相应的文本字段。

在添加了一些与这些特定文本字段和开关无关的视图、标签、按钮等之后,现在代码不再起作用,我不知道为什么。只有最后输入的文本字段数据被传递到目的地,所以如果我在三个文本字段中输入数据,只有最后输入的一个被传递。如果我再次切换开关,现在没有数据传递。

谁能帮我指出正确的方向,为什么这段代码最初有效,但现在无效?谢谢。

FirstViewController.h

import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

//Defines input value for each of the four textfields
@property (weak, nonatomic) IBOutlet UITextField *textFieldA;
@property (weak, nonatomic) IBOutlet UITextField *textFieldB;
@property (weak, nonatomic) IBOutlet UITextField *textFieldC;
@property (weak, nonatomic) IBOutlet UITextField *textFieldD;

@property (weak, nonatomic) NSString *textFieldAContent;
@property (weak, nonatomic) NSString *textFieldBContent;
@property (weak, nonatomic) NSString *textFieldCContent;
@property (weak, nonatomic) NSString *textFieldDContent;

@end

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()
@end

@implementation FirstViewController

@synthesize textFieldA;
@synthesize textFieldB;
@synthesize textFieldC;
@synthesize textFieldD;
@synthesize textFieldAContent;
@synthesize textFieldBContent;
@synthesize textFieldCContent;
@synthesize textFieldDContent;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.textFieldA.text = self.textFieldAContent;
    self.textFieldB.text = self.textFieldBContent;
    self.textFieldC.text = self.textFieldCContent;
    self.textFieldD.text = self.textFieldDContent;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"FirstToSecondView"])
    {
        SecondViewController *controller = (SecondViewController *)segue.destinationViewController;
        controller.textFieldAContent = self.textFieldA.text;
        controller.textFieldBContent = self.textFieldB.text;
        controller.textFieldCContent = self.textFieldC.text;
        controller.textFieldDContent = self.textFieldD.text;
    }
}
@end

SecondViewController.h

import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

//Defines input value for each of the four textfields
@property (weak, nonatomic) IBOutlet UITextField *textFieldA;
@property (weak, nonatomic) IBOutlet UITextField *textFieldB;
@property (weak, nonatomic) IBOutlet UITextField *textFieldC;
@property (weak, nonatomic) IBOutlet UITextField *textFieldD;

@property (weak, nonatomic) NSString *textFieldAContent;
@property (weak, nonatomic) NSString *textFieldBContent;
@property (weak, nonatomic) NSString *textFieldCContent;
@property (weak, nonatomic) NSString *textFieldDContent;

@end

SecondViewController.m

#import "SecondViewController.h"
#import "FirstViewController.h"

@interface SecondViewController ()
@end

@implementation SecondViewController

@synthesize textFieldA;
@synthesize textFieldB;
@synthesize textFieldC;
@synthesize textFieldD;
@synthesize textFieldAContent;
@synthesize textFieldBContent;
@synthesize textFieldCContent;
@synthesize textFieldDContent;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.textFieldA.text = self.textFieldAContent;
    self.textFieldB.text = self.textFieldBContent;
    self.textFieldC.text = self.textFieldCContent;
    self.textFieldD.text = self.textFieldDContent;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"SecondToFirstView"])
    {
        FirstViewController *controller = (FirstViewController *)segue.destinationViewController;
        controller.textFieldAContent = self.textFieldA.text;
        controller.textFieldBContent = self.textFieldB.text;
        controller.textFieldCContent = self.textFieldC.text;
        controller.textFieldDContent = self.textFieldD.text;
    }
}
@end

【问题讨论】:

  • 你能澄清一下“故事板”吗?您似乎在一个故事板中有 2 个 ViewController,并且在每个故事板中添加了元素。但是你一直说你有很多故事板。
  • 嗨拉姆。对不起,我用错了词。你是对的,一个故事板和 2 个 ViewController。 (当我输入原始帖子时,我错误地认为各个面板被称为故事板)。我会编辑我的帖子。谢谢。
  • 它工作正常,但现在不行......你的 segue 标识符仍然是“SecondToFirstView”/“FirstToSecondView”吗?如果那些在你的故事板编辑中被丢弃,你仍然会继续,但你会错过你的属性设置器调用。另外,不相关:您不需要 @synthesize 这里。
  • 我不知道这是否与您的问题有关,但是您不应该在segues中来回走动。当您认为要返回 FirstViewController 时,实际上是在创建该控制器的一个新实例,因此当您来回移动时,您将添加越来越多的控制器,并且它们都不会被释放。当你倒退时,你要么需要使用 unwind segue,要么使用dismissViewControllerAnimated:completion:(假设你使用的是modals segues——如果你使用的是push segues,那么使用popViewControllerAnimated:)。

标签: ios objective-c


【解决方案1】:

跟进我的问题。我从来没有弄清楚问题是什么,但是正如用户 redelmar 对原始帖子的评论,我不应该来回使用 segues(每次转换视图时内存使用量都会增加)。相反,我在一个 ViewController 上做了我想做的所有事情。感谢您的所有帮助!

【讨论】:

    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多