Getting data from an array in the controller of the previous view, put it in another TableViewController

I have 2 TableViewControllers. When the user removes the UIBarButtonItem, he saves the text from the UITextField, runnameField, in SaveViewController. It should take the text runnameField and put it in an array, runArray. In RecordVC, tableView data must come from runArray. Can someone please help me with this? I searched Google for a long time and could not find the 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 priced per ton.

Thanks,

Dhruv

+4
source share
1 answer

The moment you update RecordVC.runsArray, I would assume that your tableview is already initialized and loaded, albeit without data.

What I did recently in this very situation is to use notifications.

In your RecordsViewController, you must register to be notified in your init method:

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataLoaded:) name:@"runsArrayLoaded" object:nil]; // Custom initialization } return self; } 

Also in RecordsViewController you will need a method that is called when a notification is received. In this case, it is dataLoaded:

 -(void) dataLoaded:(NSNotification *)notif { if (notif.userInfo){ runsArray = [notif.userInfo objectForKey:@"newdata"]; [self.tableview reloadData]; } } 

The part that does all this magic work is in your saverun method inside the SaveViewController class.

 - (IBAction)saverun:(UIBarButtonItem *)sender { RecordsViewController *RecordsVC = [[RecordsViewController alloc] initWithNibName:nil bundle:nil]; RecordsVC.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; [runsArray addObject:[NSString stringWithFormat:@"%@", runnameField.text]]; [self presentModalViewController:RecordsVC animated:YES]; // notify the RecordsViewController that new data is available NSMutableDictionary* userInfo = [[NSMutableDictionary alloc] initWithCapacity:1]; [userInfo setObject:runsArray forKey:@"newdata"]; [[NSNotificationCenter defaultCenter] postNotificationName:@runsArrayLoaded" object:nil userInfo:(NSDictionary*)userInfo]; } 

Finally, you must clear and delete the notification in the RecordsViewController class. In your WillDisappear view, add the following line:

 [[NSNotifcation defaultCenter] removeObserver:self name:@"runsArrayLoaded" object:nil]; 
+1
source

Source: https://habr.com/ru/post/1445374/


All Articles