将数据从两个UItext字段传递到新的视图控制器 [英] Pass data from two UItextfields to new view controller

查看:107
本文介绍了将数据从两个UItext字段传递到新的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点卡住试图传递数据从一个视图控制器上的两个UITextfields到另一个viewcontroller。基本上我有以下项目:

I am a bit stuck on trying to pass data from two UITextfields on one viewcontroller to another viewcontroller. Basically I got the items below:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

    IBOutlet UITextfield *Textfield1;
    IBOutlet UITextfield *Textfield2;
}

-(IBAction)GoNextView:(id)sender;

@end

ViewController.m / p>

ViewController.m

@interface ViewController ()

@end

@implementation ViewController

(IBAction)GoNextView:(id)sender {

    SecondView *second = [[SecondView alloc] initWithNibName:nil bundle:nil];

    [self presentViewController:second animated:YES completion:NULL];  
}

- (void)viewDidLoad {
     [super viewDidLoad];
}

SecondView.h

#import <UIKit/UIKit.h>

@interface SecondView : UIViewController {

    IBOutlet UILabel *ShowTextfield1;
    IBOutlet UILabel *ShowTextfield2;
}

-(IBAction)GoBack:(id)sender;

@end

SecondView.m / p>

SecondView.m

@interface SecondView ()

@end

@implementation SecondView

- (IBAction)GoBack:(id)sender {

    [self dismissViewControllerAnimated:YES completion:NULL];
}

我想要的是,当按钮 GoNextView ,它应该将 UITextfield 中的数据传递给 SecondView 。并转到下一页。数据现在应显示在 SecondView 上的两个标签中。我一直在努力这个很多个小时,它杀了我。我知道我必须使用一些 NSString @property(nonatomic,retain)NSString * asdas 。但我根本不能使它的工作。因此,基于上面的代码如何将数据从两个 UITextfield 传递到下一个视图的两个标签?

What I want is, that when the button GoNextView is pressed it should pass the data in the UITextfield to the SecondView. And the go to the next page. The data should now be displayed in the two Labels on the SecondView. I've been struggling with this for many hours now and its killing me. I know I have to use some NSString and @property(nonatomic, retain) NSString *asdas. But I simply can't make it work. So based on the above code how do I pass data from the two UITextfield to the two labels on the next view?

推荐答案

使用NSUserDefaults或普通合成公共接口变量。例如,

Use NSUserDefaults or the normal synthesising common interface variables. For example,


  1. NSUserDefaults
    从发件人的视图控制器,

  1. NSUserDefaults From sender's view controller,

NSString * text = textField1.text;
NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:text forKey:@text1value];

NSString *text=textField1.text; NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults]; [prefs setObject:text forKey:@"text1value"];

从任何视图控制器as,

And you can get the value from any view controller as,

NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];
NSString *received string= [prefs stringForKey:@"text1value"];




  1. 正常指派者,

我相信你必须使用performsegue来导航视图控制器。有一个称为prepareforsegue的委托方法,您可以将textfield值分配给接口变量。例如,

I believe you must be using performsegue to navigate view controllers. There is a delegate method called "prepareforsegue" and you can assign the textfield values into the interface variables. For example,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"seguename"]) {
        destViewController *destViewController = segue.destinationViewController;
        destViewController.name = @"some text";
    }
}

在接收器的视图控制器中,getter变量实际上被定义as

In the receiver's view controller, getter variable is actually defined as,

在.h文件中, @property(nonatomic,strong)NSString * name;

在.m文件中,不要忘记在实现之后合成变量

In .m file, Don't forget to synthesis the variable after implementation

@implementation destViewController
@synthesize name;

希望它有帮助!

这篇关于将数据从两个UItext字段传递到新的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆