Ios 在情节提要中的ViewController之间传递信息

Ios 在情节提要中的ViewController之间传递信息,ios,xcode,storyboard,Ios,Xcode,Storyboard,我正在通过制作一个非常简单的应用程序来学习如何使用故事板。在主视图控制器(InfoViewController)上,我有一个名为nameField的文本字段。在该字段中输入文本后,当我输入保存按钮时,该文本应附加到数组(列表)(在TableViewController中声明),并显示在TableViewController中的表格上 此外,segue标识符是:GoToTableViewController 但是,文本不会从名称字段传递到列表(数组)。起初,我认为我在文本字段上犯了一些错误。所以我

我正在通过制作一个非常简单的应用程序来学习如何使用故事板。在主视图控制器(
InfoViewController
)上,我有一个名为
nameField
的文本字段。在该字段中输入文本后,当我输入
保存
按钮时,该文本应附加到数组(
列表
)(在
TableViewController
中声明),并显示在
TableViewController
中的表格上

此外,segue标识符是:
GoToTableViewController

但是,文本不会从
名称字段
传递到
列表
(数组)。起初,我认为我在文本字段上犯了一些错误。所以我用静态文本替换了它。但这也无济于事。然后,我使用NSLog()检查字符串是否已添加到数组中,但每次我得到
Null
。据我所知,在加载
TableViewController
之前,不会创建
列表(数组)。因此,我在InfoViewController中设置了alloc和init。但这没有帮助

谁能帮我找出我犯的错误吗

谢谢

我的守则的相关章节如下:

InfoViewController.h

#import <UIKit/UIKit.h>

@class TableViewController;

@interface InfoViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (strong, nonatomic) TableViewController *tableViewController;
@end
(*)我插入了这些语句,以查看在nameField中使用值是否有误。 (**)此语句用于检查数组中插入的值

TableViewController.h

#import <UIKit/UIKit.h>

@class InfoViewController;

@interface TableViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *list;
@property (strong, nonatomic) InfoViewController *infoViewController;

@end

视图中重新加载表格将显示
表格视图控制器的方法

[tableViewController.tableView reloadData];

我看到的第一件事是,您仍然在viewDidLoad中为您的表视图控制器分配一个新的列表数组,实际上放弃了您在infoViewController中所做的工作?nameField.text是有效字符串吗?tableViewController.list是一个初始化的数组吗?当您希望它时,它是否有内容?如果确定这些情况属实,请将table view controller的list属性设置为infoViewController在prepareForSegue实现中拥有的数组,并且不要初始化新数组来替换要在table view controller的viewDidLoad中使用的数组,杰拉尔德·威廉:谢谢你的回复,我对延迟回复表示歉意。
prepareforsgue:
中的NSLog()读取
(null)
。由于TableViewController是在按下
保存
按钮后加载的,因此我不希望创建
列表
。因此,在InfoViewController的ViewDiLoad中,I
alloc
init
。但是,它没有帮助,并且该信息不会传递到
列表
数组。根据你的评论,我有了一些方向,所以让我先尝试自己解决这个问题。如果我不能解决它,我会给你回电话的。再次感谢@杰拉尔德威廉:我的计划终于成功了!我在
InfoAppDelegate
中声明了
list
,并将
InfoViewController
TableViewController
list
连接到AppDelegate的
list
。这彻底解决了问题。我将通过github与新手开发者分享我的经验。谢谢你们的回复。我的程序终于成功了。请参考我对杰拉尔德威廉评论的最后回应。
#import "TableViewController.h"
#import "InfoViewController.h"

@implementation TableViewController

@synthesize list;
@synthesize infoViewController;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    list = [[NSMutableArray alloc] init];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ return 1; }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ return list.count; }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [list objectAtIndex:indexPath.row];

    return cell;
}

@end
[tableViewController.tableView reloadData];