Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing information between viewControllers in Storyboard
    primarykey
    data
    text
    <p>I am learning how to use storyboards by making a very simple app. On the main view controller (<code>InfoViewController</code>), I have a textField by the name: <code>nameField</code>. After entering text in this field, when I enter the <code>save</code> button, the text should should get appended to the array (<code>list</code>) (declared in <code>TableViewController</code>) and be displayed on the table in <code>TableViewController</code>.</p> <p>Also, the segue identifier is: <code>GoToTableViewController</code>.</p> <p>However, the text does not get passed from <code>nameField</code> to the <code>list</code> (array). At first, I assumed that I was making some mistake with the textField. So I replaced it with a static text. But that did not help either. Then I checked if the string has been added to the array by using NSLog() , but every time I get <code>Null</code>. From my understanding, the <code>list</code> (array) is not created until <code>TableViewController</code> is loaded. For that reason, I <code>alloc</code> and <code>init</code> <code>list</code> in InfoViewController. But it does not help.</p> <p>Can somebody please help me find out the mistake that I am making?</p> <p>Thanks!</p> <p>Relevant sections of my code are as follows:</p> <p>InfoViewController.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @class TableViewController; @interface InfoViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *nameField; @property (strong, nonatomic) TableViewController *tableViewController; @end </code></pre> <p>InfoViewController.m</p> <pre><code>#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 </code></pre> <p>( * ) I inserted these statements to see if I was making a mistake with using the value in nameField. ( ** ) This statement is meant to check the value inserted in the array.</p> <p>TableViewController.h </p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @class InfoViewController; @interface TableViewController : UITableViewController @property (nonatomic, strong) NSMutableArray *list; @property (strong, nonatomic) InfoViewController *infoViewController; @end </code></pre> <p>TableViewController.m</p> <pre><code>#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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COthe first thing I see is that you are still alloc-init'ing the a new list array for your table view controller in viewDidLoad, effectively throwing away the work you did in infoViewController.
      singulars
    2. COWhat did your NSLog in prepareForSegue tell you? Is nameField.text a valid string? Is tableViewController.list an initialized array and does it have contents when you expect it to? If you make certain that these things are the case, set the table view controller's list property to the array that infoViewController owns in your prepareForSegue implementation, and don't initialize a new array to replace the one you mean to use in the table view controller's viewDidLoad, it should work as intended.
      singulars
    3. CO@geraldWilliam: Thanks for your response and my apologies for the delay in responding. The NSLog() in `prepareForSegue:` read `(null)`. Since TableViewController is loaded after the `Save` button is pressed, I do not expected `list` to have been created. For that reason, I `alloc` and `init` in viewDidLoad of InfoViewController. However it does not help and the info does not get passes to the `list` array. Based on your comments, I have got some direction, so let me try sorting out the problem myself first. If I cannot solve it, I'll get back to you. Thanks once again!
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload