我有点坚持尝试将数据从一个视图控制器上的两个 UITextfield 传递到另一个视图控制器。基本上我得到了以下物品:
视图控制器.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UITextfield *Textfield1;
IBOutlet UITextfield *Textfield2;
}
-(IBAction)GoNextView:(id)sender;
@end
视图控制器.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];
}
第二视图.h
#import <UIKit/UIKit.h>
@interface SecondView : UIViewController {
IBOutlet UILabel *ShowTextfield1;
IBOutlet UILabel *ShowTextfield2;
}
-(IBAction)GoBack:(id)sender;
@end
SecondView.m
@interface SecondView ()
@end
@implementation SecondView
- (IBAction)GoBack:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
}
我想要的是,当按钮GoNextView
被按下它应该传递数据UITextfield
to the SecondView
。然后转到下一页。数据现在应该显示在屏幕上的两个标签中SecondView
。我已经为此苦苦挣扎了好几个小时,这简直要了我的命。我知道我必须使用一些NSString
and @property(nonatomic, retain) NSString *asdas
。但我根本无法让它发挥作用。那么根据上面的代码我如何从两者传递数据UITextfield
到下一个视图上的两个标签?
使用 Storyboard Segues,而不是通过函数导航到第二个视图。创建故事板应用程序。要创建 Segue,您需要一个导航视图控制器。您可以通过以下步骤来做到这一点。
- 选择第一个视图控制器并转到编辑器 -> 嵌入 -> 导航控制器。
- 然后按住 Ctrl 键并单击“GoNextView”按钮,然后拖放到 SecondView 中。
- 您将获得三个选项:推送、模型、自定义。单击“push”,然后将在这两个视图之间创建一个segue。
- 单击该segue,然后单击右侧属性窗口中的“显示属性检查器”。现在将其命名为“连接器”或您喜欢的任何名称。
现在,在 SecondView.h 中创建两个字符串,
@property (nonatomic, retain) NSString *str1;
@property (nonatomic, retain) NSString *str2;
并在 SecondView.m 中合成它们。
让我们将 ViewController 中的 UITextFields 命名为“text1”和“text2”。转到 ViewController.h 并添加#import "SecondView.h"
在 ViewController.m 中执行以下操作:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"connector"])
{
ViewController *VC = [segue destinationViewController];
VC.str1 = text1.text;
VC.str2 = text2.text;
}
}
现在您可以在 SecondView 中显示这些字符串。在 Secondview.m 中,
标签.text = str1;
然后就会显示出来。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)