简体   繁体   English

目标C-通过视图控制器传递多个文本字段(或将它们转换为可变数组)以显示为标签

[英]Objective C - Passing multiple text fields (or converting them to a mutable array) through view controllers to display as labels

I'm relatively new to Objective C and currently having the above issue. 我对Objective C比较陌生,目前遇到上述问题。 I've followed tutorials and tried to find the relevant topics here and I can get it to work for one variable, however I need to pass more than that between view controllers. 我已按照教程进行操作,并尝试在此处找到相关主题,我可以使它适用于一个变量,但是我需要在视图控制器之间传递更多信息。

I'm trying to avoid using global variables, saving onto a temp file, and singleton (pattern)(?) (I hear it can get messy). 我试图避免使用全局变量,保存到临时文件中,以及单例(pattern)(?)(我听说它会变得凌乱)。 I'm also trying to avoid delegating (If that's the right terminology) as I get the impression that it's becoming overcomplicated. 我还试图避免委派(如果这是正确的术语),因为我觉得它变得过于复杂。 In addition, if the below works for one, then it shouldn't be far off working for multiple. 另外,如果以下内容适用于一个,那么适用于多个对象应该相差不远。

I've explored both methods as posted in the code below. 我已经探究了下面代码中发布的两种方法。 Firstly by trying to pass each text field separately, initially through one controller name, then multiple. 首先,通过尝试分别传递每个文本字段,首先传递一个控制器名称,然后传递多个。

Secondly, by collating all the strings into a mutable array, and trying to pass this array through the controller, then splitting up this array back into strings/text for the label text. 其次,通过将所有字符串整理到一个可变数组中,并尝试将该数组传递给控制器​​,然后将该数组拆分回标签文本的字符串/文本。 Both attempts only pass on the info for what was the active textfield after pressing the Done button. 两次尝试仅在按下“完成”按钮后才传递活动文本字段的信息。

I've played around between weak and strong properties and that hasn't made much of a difference. 我在弱属性和强属性之间玩过,并没有太大的区别。

The storyboard has 2 view controllers (ViewController and secondViewController), each with their own navigation controllers, and a show segue between them. 情节提要有2个视图控制器(ViewController和secondViewController),每个都有自己的导航控制器,并且它们之间有显示顺序。 ViewController has 2 text fields. ViewController有2个文本字段。 secondViewController has 2 labels. secondViewController有2个标签。

Code as follows: 代码如下:

ViewController.h (for both strings and array method) ViewController.h(用于字符串和数组方法)

#import <UIKit/UIKit.h>
#import "secondViewController.h"

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *textFieldData;

@property (weak, nonatomic) IBOutlet UITextField *textFieldDataTwo;

@end

ViewController.m (first method): (Added prepareforSegue) ViewController.m(第一个方法):(添加了prepareforSegue)

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    UINavigationController *navController = (UINavigationController *)segue.destinationViewController;

    //UINavigationController *navController1 = (UINavigationController *)segue.destinationViewController;

    secondViewController *svc = (secondViewController *)navController.topViewController;
    secondViewController *svc1 = (secondViewController *)navController.topViewController;
    //Also tried navController1.topViewController with previous comment

    svc.labelText = _textFieldData.text;
    svc1.labelTwoText = _textFieldDataTwo.text;

}

secondViewController.h (for first method) secondViewController.h(用于第一种方法)

# import <UIKit/UIKit.h>

@interface secondViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *labelSceneTwo;
@property (weak, nonatomic) IBOutlet UILabel *labelTwoSceneTwo;

@property (weak, nonatomic) NSString *labelText;
@property (weak, nonatomic) NSString *labelTwoText;

@end

secondViewController.m (First method) secondViewController.m(第一个方法)

(Only added to viewDidLoad) (仅添加到viewDidLoad中)

- (void)viewDidLoad {
    [super viewDidLoad];

    _labelSceneTwo.text = _labelText;
    _labelTwoSceneTwo.text = _labelTwoText;

}

(I'll hopefully update this post for the second method tomorrow morning). (我希望明天早上将这篇文章更新为第二种方法)。

Thank you for your time. 感谢您的时间。

Update 1: 更新1:

for method 2, I'm currently getting an uncaught exception of type NSException. 对于方法2,我目前遇到了NSException类型的未捕获异常。

In ViewController.m under prepareForSegue 在ViewController.m中的prepareForSegue下

UINavigationController *navController = (UINavigationController *)segue.destinationViewController;

secondViewController *svc = (secondViewController *)navController.topViewController;

NSMutableArray *textFieldMArray = [NSMutableArray arrayWithObjects:_textFieldData, _textFieldDataTwo, nil];

textFieldMArray = [[NSMutableArray alloc] init];

svc.labelTextMArray = textFieldMArray;

secondViewController.h secondViewController.h

Added: 添加:

@property (strong,nonatomic) NSMutableArray *labelTextMArray;

secondViewController.m secondViewController.m

In view did load: 鉴于确实加载:

_labelTextMArray = [[NSMutableArray alloc] init];

//The line below is what is causing the termination, otherwise, screen shows 2 labels each saying "Label"
//If property of array is set to weak, then both labels appear blank.

_labelSceneTwo.text = [_labelTextMArray objectAtIndex:0];

_labelTwoSceneTwo.text = [_labelTextMArray objectAtIndex:1];

Final working code: 最终工作代码:
For method 2: 对于方法2:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {  
    UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
    secondViewController *controller = (secondViewController *)navController.topViewController;

    NSMutableArray *textFieldMArray = [NSMutableArray arrayWithObjects:_textFieldData.text, _textFieldDataTwo.text, nil];
    controller.labelTextMArray = textFieldMArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _labelSceneTwo.text = [_labelTextMArray objectAtIndex:0];
    _labelTwoSceneTwo.text = [_labelTextMArray objectAtIndex:1];
)

For method 1: 对于方法1:
Pretty much the same code, make sure the strings are strong as well, eg remove the weak . 几乎相同的代码,请确保字符串也很strong ,例如,删除weak

How we got there 我们如何到达那里
First I would suggest changing your prepareForSeque method quite a bit. 首先,我建议大量更改您的prepareForSeque方法。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    //include the code for the navigationController
    secondViewController *destinationController = (secondViewController *)segue.destinationViewController;
    destinationController.labelText = self.textFieldData.text;
    destinationController.labelTwoText = self.textFieldDataTwo.text;
}

You should give the class a name that starts with an upper case letter like SecondViewController , that is just basic coding convention. 您应该给该类一个以大写字母开头的名称,例如SecondViewController ,这只是基本的编码约定。

Regarding option 2: 关于选项2:

NSMutableArray *textFieldMArray = [NSMutableArray arrayWithObjects:_textFieldData, _textFieldDataTwo, nil];
svc.labelTextMArray = textFieldMArray;

You should remove the line _labelTextMArray = [[NSMutableArray alloc] init]; 您应该删除_labelTextMArray = [[NSMutableArray alloc] init]; in prepareForSegue and in viewDidLoad. 在prepareForSegue和viewDidLoad中。

And I am not sure if navController.topViewController works the way you expect it to, I dont know if the topViewController is already updated. 而且我不确定navController.topViewController是否按照您期望的方式工作,我不知道topViewController是否已经更新。 I would recommend getting the new viewController the way I already wrote down. 我建议以我已经记下的方式获取新的viewController。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM