我创建了两个视图,每个视图有四个文本字段。我有一个开关,允许我在这两个视图之间来回切换。当激活/取消激活开关时,它会将当前文本字段数据传递到目标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
答案 0 :(得分:0)
跟进我的问题。我从来没有弄清楚问题是什么,但是当用户redelmar评论到原始帖子时,我不应该使用segue来回(每次我转换视图时内存使用率都会上升)。相反,我在一个ViewController上做了我打算做的所有事情。谢谢你的帮助!