I have 2 TableViewControllers. When the user taps a UIBarButtonItem, It saves the text from a UITextField, runnameField, in SaveViewController. It should take runnameField’s text and put it in an array, runsArray. In RecordVC, the tableView’s data should come from runsArray.
Can someone please help me with this? I’ve been googling for a long time and can’t find any answers.
SaveViewController.h:
#import <UIKit/UIKit.h>
#import "RecordsViewController.h" //Next ViewController
@interface SaveViewController : UITableViewController <UITableViewDataSource,UITableViewDelegate> {
__weak IBOutlet UITextField *runnameField;
RecordsViewController *RecordsVCData;
}
@property (weak, nonatomic) IBOutlet UITextField *runnameField;
@property (weak, nonatomic) IBOutlet UITextView *runnotesTextView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveBarItem;
@property (nonatomic, retain) RecordsViewController *RecordsVCData;
- (IBAction)saverun:(UIBarButtonItem *)sender;
- (IBAction)goAwayKeyboard:(id)sender;
@end
SaveViewController.m:
- (IBAction)saverun:(UIBarButtonItem *)sender {
RecordsViewController *RecordsVC = [[RecordsViewController alloc] initWithNibName:nil bundle:nil];
RecordsVC.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
self.RecordsVCData = RecordsVC;
[RecordsVC.runsArray addObject:[NSString stringWithFormat:@"%@", runnameField.text]];
[self presentModalViewController:RecordsVC animated:YES];
}
- (IBAction)goAwayKeyboard:(id)sender {
[sender resignFirstResponder];
}
RecordsViewController.h:
#import <UIKit/UIKit.h>
@interface RecordsViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate> {
NSMutableArray *runsArray;
}
@property (weak, nonatomic) NSString *runname;
@property (strong, nonatomic) NSString *runnotes;
@property (weak, nonatomic) UITableViewCell *cell;
@property (retain,nonatomic) NSMutableArray *runsArray;
@end
RecordsViewController.m:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
// Identifier for retrieving reusable cells.
static NSString *cellIdentifier = @"MyCellIdentifier";
// Attempt to request the reusable cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// No cell available - create one
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
[cell.textLabel setText:[NSString stringWithFormat:@"%@",[runsArray objectAtIndex:indexPath.row]]];
}
// Set the text of the cell to the runnamestring.
[cell.textLabel setText:[NSString stringWithFormat:@"%@",[runsArray objectAtIndex:indexPath.row]]];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [runsArray count];
}
Any help is appreciated a ton.
Thanks,
Dhruv
At the point where you are updating RecordsVC.runsArray, I would imagine your tableview has already been initialized and loaded, albeit with no data.
What I have done recently in this very situation is to use notifications.
In your RecordsViewController, you would register for a notification in your init method:
Also in the RecordsViewController, you will need the method that gets called when the notification is received. In this case, it is dataLoaded:
The part that makes all this magic works is in your saverun method inside the SaveViewController class.
Finally, the you must clean up and remove the notification observation in the RecordsViewController class. In your viewWillDisappear, add the following line: