将数组发送到viewcontroller iOS xcode(Send array to viewcontroller iOS xcode)

编程入门 行业动态 更新时间:2024-10-26 16:23:28
数组发送到viewcontroller iOS xcode(Send array to viewcontroller iOS xcode)

我有2个视图控制器:

索引秒在索引中我有2个文本字段和按钮。

我填写此文本字段并按“下一步按钮”进入第二个视图,我需要“显示”这两个文本字段,通过使用Array(稍后我将发送JSON数据)。

我试过这个:在第二类中声明一个@property作为textArray。 然后在从Index推送到Second时,您需要设置此属性。

在Second.h文件中,

@property(nonatomic, strong) NSArray *textArray;

在Index.m文件中,

Second *aSecond = [[Second alloc] init]; aSecond.textArray = @[textField1.text, textField2.text];

//编写代码以从Index推送到Second现在在类Second中,您可以将它用作aSecond.textArray [0]和aSecond.textArray [1]

但是,如果我切换页面(单击下一步)阵列已被取消。

对于切换页面我使用:

SecondPage *SecondPage = [self.storyboard instantiateInitialViewController]; SecondPage = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondPage"]; [self SecondPage animated:YES];

我怎么能最好地实现? 请帮忙! 最好的问候伊万。

I have 2 view controller:

Index Second In Index I have 2 textfield and button.

I fill this text field and press "next button" to second view, I need to "show" this two text field, by using Array (later I will send JSON data).

I Tried this: Declare an @property in class Second as textArray. And then while pushing to Second from Index, you need to set this property.

In Second.h file,

@property(nonatomic, strong) NSArray *textArray;

In Index.m file,

Second *aSecond = [[Second alloc] init]; aSecond.textArray = @[textField1.text, textField2.text];

//write code to push from Index to Second Now in class Second, you can use it as aSecond.textArray[0] and aSecond.textArray[1]

But if i switch page (click next) array has been nulled.

For switch page i use this:

SecondPage *SecondPage = [self.storyboard instantiateInitialViewController]; SecondPage = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondPage"]; [self SecondPage animated:YES];

How I can best realize? Please help! Best regards Ivan.

最满意答案

有很多方法可以将值从一个控制器传输到另一个控制器,但对于您而言,我认为这将是最简单的。 使用NSUserDefaults

第一个简单的方法

在第一个控制器

[[NSUserDefaults standardUserDefaults] setObject:@[textField1.text, textField2.text] forKey:@"listOfData"];

在第二控制器

NSArray *array = [[NSUserDefaults standardUserDefaults]objectForKey:@"listOfData"];

第二个简单的方法是

DetailVC *detailVC = [self.storyboard instantiateInitialViewController]; detailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailVC"]; detailVC.textArray = @[textField1.text, textField2.text];

There are lot of ways to transfer the value from one controller to an other, but for you I think this will be the most simple ever. Use NSUserDefaults

First Easy Ways

in first Controller

[[NSUserDefaults standardUserDefaults] setObject:@[textField1.text, textField2.text] forKey:@"listOfData"];

in 2nd Controller

NSArray *array = [[NSUserDefaults standardUserDefaults]objectForKey:@"listOfData"];

2nd Easy ways is

DetailVC *detailVC = [self.storyboard instantiateInitialViewController]; detailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailVC"]; detailVC.textArray = @[textField1.text, textField2.text];

更多推荐

本文发布于:2023-08-03 14:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1389264.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   发送到   viewcontroller   iOS   Send

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!