将数据从一个ViewController中的某些TextFields传递到另一个ViewControl中的TextVie



我是iOS的新手。我正在为iOS做一个项目,遇到了一些困难。我相信这个问题应该有一个简单的解决方案,但我花了几个小时在这个网站上查看教程和其他提交的内容,但到目前为止,一切都没有奏效。

我的问题是,我在ViewControllerS中有5个TextFields,我想从这些TextFields中获取数据,并将它们组合到ViewControllerView中的TextView中。这是我的密码。

ViewControllerS.h:

#import <UIKit/UIKit.h>
@interface ViewControllerS : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *txtfDay;
@property (weak, nonatomic) IBOutlet UITextField *txtfMonth;
@property (weak, nonatomic) IBOutlet UITextField *txtfName;
@property (weak, nonatomic) IBOutlet UITextField *txtfStart;
@property (weak, nonatomic) IBOutlet UITextField *txtfEnd;
@property (weak, nonatomic) IBOutlet UIButton *btnFinish;
@end

ViewControllerS.m:

#import "ViewControllerS.h"
@interface ViewControllerS ()
@end
@implementation ViewControllerS
@synthesize txtfDay;
@synthesize txtfMonth;
@synthesize txtfName;
@synthesize txtfStart;
@synthesize txtfEnd;
@synthesize btnFinish;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
}

@end

ViewControllerView.h:

#import <UIKit/UIKit.h>
@interface ViewControllerView : UIViewController
@property (weak, nonatomic) IBOutlet UITextView *txtViewFinal;
@end

ViewControllerView.m:

#import "ViewControllerView.h"
@interface ViewControllerView ()
@end
@implementation ViewControllerView
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
}
@end

好吧,正如你所看到的,这基本上只是我程序的框架。当我遵循教程,尝试这里的想法,并听取同学们的意见时,我对这个代码做了很多修改。到目前为止,一切都不起作用,但我知道这是一个简单的解决方案。我喜欢使用NSUserDefaults,但如果有人有任何其他想法,我也会尝试。同样,我只是想弄清楚如何使用TextFields和TextView将数据从一个ViewController传递到另一个。

我不需要5个人的路,只需要1个人的路。如果我只是朝着正确的方向轻推一下,我就能弄清楚格式和其他4个。

谢谢你对我的帮助!我真的很喜欢用Xcode编程,但这也很令人沮丧。谢谢

它不起作用,因为在创建时,您的ViewControllerView实例仍然没有加载UI组件,所以即使您将其直接传递给textView,它也会在此时被忽略。

在ViewControllerView.h中创建一个字典来传递ViewControllerS中的数据,如下所示:

@property (nonatomic, strong) NSDictionary *dicTextData;

并将数据设置到其中。完成此操作后,只需从ViewControllerView的-(void)viewDidLoad中加载此数据,瞧!

更新

ViewControllerS.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:@"ViewControllerView"]) {
ViewControllerView *destination = segue.destinationViewController;
destination.dicTextData = @{@"day":self.txtfDay.text,
@"Month":self.txtfMonth.text,
@"Name":self.txtfName.text}; //and so on...
}
}

ViewControllerView.m

- (void)viewDidLoad {
self.txtViewFinal.text = [NSString stringWithFormat:@"My name is %@", [self.dicTextData objectForKey:@"Name"]];
//and so on... 
}

请注意,我并没有在示例中使用所有字段,但我相信您已经明白了。好的编码!

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:@"ViewControllerView"]){
ViewControllerView *destination = segue.destinationViewController;
destination.finalText = self.txtfDay.text;
}

请确保您正确设置了segue的标识符。

编辑:

谢谢@rmaddy指出问题:

出于解耦的目的,存储将通过属性传递的数据并让视图控制器处理这些数据将是一种更好的方法。在这种情况下,假设有一个NSString类型的属性,称为finalTextprepareForSegue方法存储数据,现在您可以自由处理这些数据。

有多种方法可以做到这一点。另一种方法是使用AppDelegate对象。遵循步骤-

  1. 在AppDelegate.h文件-中写入这一行

    @property (nonatomic, strong) NSString *combinedText;
    
  2. 在ViewControllerViewS.m文件中导入AppDelegate文件。如-

    #import "AppDelegate.h"
    
  3. 在ViewControllerViewS.m-的viewDidLoad中写入行

    appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    
  4. 现在在btnFinish按钮的正文中写下这行-

    appDelegate.combinedText=[NSString stringWithFormat:@"fDay:%@,fMonth:%@,fName:%@,fStart:%@,fEnd:%@", 
    self.txtfDay.text, self.txtfMonth.text, self.txtfName.text, self.txtfStart.text, self.txtfEnd.text];
    
  5. 现在,在目标ViewController.m文件中,重复步骤2和3。

  6. 现在将组合文本值分配给textView.Like-

    UITextView *textView=[[UITextView alloc]init];
    textView.text=appDelegate.combinedText;
    

如果适用,请告诉我。非常感谢。

相关内容