我正在学习如何通过制作一个非常简单的应用程序来使用storyboard。在主视图控制器(InfoViewController
)上,我有一个名为nameField
的textField。在此字段中输入文本后,当我输入save
按钮时,应将文本附加到数组(list
)(在TableViewController
中声明)并显示在TableViewController
的表格中。在Storyboard中的viewControllers之间传递信息
此外,segue标识符是:GoToTableViewController
。
但是,文本不会从nameField
传递到list
(数组)。起初,我认为我在textField上犯了一些错误。所以我用静态文本替换了它。但那也没有帮助。然后我检查是否已经使用NSLog()将该字符串添加到数组中,但是每次我得到Null
。从我的理解,list
(阵列)不会创建,直到加载TableViewController
。出于这个原因,我在InfoViewController中输入了alloc
和init
list
。但它没有帮助。
有人能帮我找出我正在犯的错误吗?
谢谢!是
我的代码的相关章节如下:
InfoViewController.h
#import <UIKit/UIKit.h>
@class TableViewController;
@interface InfoViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (strong, nonatomic) TableViewController *tableViewController;
@end
InfoViewController.m
#import "InfoViewController.h"
#import "TableViewController.h"
@implementation InfoViewController
@synthesize nameField;
@synthesize tableViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tableViewController = [[TableViewController alloc] init];
tableViewController.list = [[NSMutableArray alloc] init];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqual:@"GoToTableViewController"])
{
/* Pass data to list and then reloadTable data */
tableViewController = segue.destinationViewController;
tableViewController.infoViewController = self;
// (*) [tableViewController.list addObject:nameField.text];
// (*) [tableViewController.list addObject:@"Hi!"];
[tableViewController.list insertObject:@"Hi" atIndex:0];
// (**) NSLog(@"%@", [tableViewController.list objectAtIndex:0]);
[tableViewController.tableView reloadData];
}
}
@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.m
#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
我看到的第一件事情是,你仍然ALLOC - 在viewDidLoad中为您的表视图控制器启动一个新的列表数组,有效地抛弃您在infoViewController中所做的工作。 – geraldWilliam
你的NSLog在prepareForSegue中告诉你什么? nameField.text是一个有效的字符串吗? tableViewController.list是一个初始化的数组,当你期望它有内容吗?如果你确定这些事情是真的,那么将table view controller的list属性设置为你的prepareForSegue实现中拥有的数组,并且不要初始化一个新数组来替换你在表视图控制器中使用的数组viewDidLoad,它应该按预期工作。 – geraldWilliam
@geraldWilliam:感谢您的回复,并对延迟回复表示歉意。 'prepareForSegue:'中的NSLog()读取'(null)'。由于TableViewController是在按下'Save'按钮后加载的,我不希望'list'被创建。出于这个原因,我在InfoViewController的viewDidLoad中'alloc'和'init'。然而它没有帮助,而且信息没有传递给'list'数组。根据你的意见,我已经有了一些方向,所以让我先试着自己解决问题。如果我解决不了,我会回复你的。再次感谢! –