Passing NSMutableArray back to previous view controller using delegates - ios

I have 3 view controllers
The first view controller is storing objects in an NSMutableArray from text fields, info entered by user. There are 4 fields which are being stored as one object in an NsMutableArray.
After storing the data in the array, it is then being stored in another array, array2, which is located in the 2nd view controller, the 2nd view controller has a UItableview and custom tableview cell. The array2 is displaying the information in the tableview cell.
I have an update btn which goes to the 3rd view controller and displays all the information in the tableview cell in 4 uitextfields just like in the 1st view controller. After I change the data, I am storing it again in another array, array3 which I want to pass back to the previous 2nd view controller to display the updated info in the table view cell.
Everything is working fine up till now, my question is, how can I pass the array3 back to the previous view controller to show the updated info? I am using delegates to pass data but I cannot seem to pass it back to the previous view controller. the app crashes and the error is "object cannot be nil"
I have checked various tutorials and questions but cannot find the answer. Please help.
Using delegates and arrays

You have to pass array in your delegate method so you can get this array in second viewcontroller. For example you have called delegate method like :
[self.delegate setData:(NSMutableArray *)array];
You can get this array where you define setData method like
(void)setData:(NSMutableArray *)array {
NSLog(#"Array value %#",array)
}

My suggestion is you can maintain your NSMutableArray in Appdelgate OR
Using the NSUserdefaults
-By using these you can maintain your data anywhare in the project

Related

How to pass data back to the original UINavigationController

I would like to pass some data from the last Table View to the First View. How can I do grab a hold of the First View object? I'm familiar with delegate pattern.
From the First View, I'm using Style Modal to invoke the Table View.
Create an object to manage your data model outside of your controller structure (singleton or owned by the application delegate). Update it when you have new data and read from it when you want to display something. Then, instead of having to make links between controllers, all you need to do is remove the one or ones you don't want and let the one you go back to decide what to show.
to get your rootViewController of your navigation controller, you can try this
NSArray *viewControllers = self.navigationController.viewControllers;
YourRootViewController *rootViewController = (YourRootViewController *)[viewControllers objectAtIndex:viewControllers.count - 2];
and add your data to rootViewController

How do I refresh a table view from a separate uiviewcontroller?

In my project I have two view controllers one that lists all the core data stored in a table view and a second that allows the user to input data to be stored. However when I click the back button on the navigation controller on the second it fails to refresh the tableView.
I have tried [self.tableView reloadData] in the viewDidLoad as a bit of a long shot but no joy.
How would i go about refreshing the data when the user clicks back?
Many thanks
Danny
The shortest route would be to move [self.tableView reloadData] from viewDidLoad to viewDidAppear but I would suggest using a delegate or callback so that it only reloads when necessary.

Advice on data reload between Tableview and viewcontroller

This is just to as for advice for the best way to do the following;
I have a tableview which is populated by two arrays; when rowatindexpath is selected takes me to another viewcontroller which displays the data. All is working fine: however, I need to move back to the tableview to display the same data (which now has been released). which would be the best procedure to call back the data? do I pass the data back and forth between the tableview everytime or write the data to a plist and call it back on tableview reload.
Thanks for your time!
To pass data down a stack of controllers the usual approach is to use a delegate protocol on the 'detail' VC that is implemented by the 'master' VC.
It's a common question - I've got an example application that demonstrates this https://bitbucket.org/abizern/delegation-example
Which reminds me, I should update it for iOS 7.
It can easily be achieved by reloadData in viewWillAppear: method.
make a NSMutableArray in SingletonClass or in your application's AppDelegate and update/delete values from it from your DetailView
Once you are in your UITableView VC just reloadData in viewWillAppear:

Inserting rows in table view iOS 6+

So I have seen a few tutorials out there on how to do this but really they all involve hard-coding buttons and text fields in. What I have done is created a view controller A, with an embedded table view. And in the navigation bar I have added a button (the "Add") plus symbol button. This button I have made its action to modally bring up another view controller B where I have a text field and a button Save.
Here is a picture for reference:
So what I want to do, is have the user be able to press the green plus button in view controller A, and then it will bring up view controller B where I can enter a username, press save, and it will dismiss itself (I know how to do this) and reveal A again but now showing the newly added contact.
I am using an NSMutableArray to store and populate the table. What is the simplest, and easiest way in which I can do this, without having to hardcode buttons in as shown in the apple docs Table View Programming Guide. I have two separate classes for A I have the Contacts class and for B I have designated it the addContact class. Both classes are simply view controller subclasses.
First I would push the second view instead of presenting it modally.
To the Segue (connector between first and second view) you can also attach an identifier.
https://developer.apple.com/library/ios/recipes/xcode_help-interface_builder/articles-storyboard/StoryboardSegue.html
Additionally you can perform some action in the prepareForSegue method before the view is pushed.
In this method I would then pass the array and tableview, such that you can add the username and reload the table when the save button is pressed.
http://nscookbook.com/2013/01/ios-programming-recipe-5-passing-values-between-segues-with-prepareforsegue/
The scope of your question isn't very clear, but assuming you know how to pass the saved value back to the parent view controller, you would insert the table row using something like this:
- (void)addItem:(id)item { // where item is the object being added to the data model
[self.items addObject:item]; // where self.items is your mutable array
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.items.count - 1 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
The insertRowsAtIndexPaths:withRowAnimation: method informs the table view that you've added new items to your data model at the given index paths and it will then call your data source methods, including cellForRowAtIndexPath, to generate the new cell and animate it into place.

UINavigationController not transferring data

I have an tabbed application that captures data, stores it using NSUserDefaults and presents a readout in a table that is contained in a UIViewController. I am using a navigation controller between the tab view controller and the data view. The data view is then connected to a history view controller which is a child view controller. The history also has a table.
When you press a row in the history view, the app is supposed to transition back to the data view and present the readout for the history. This is done by making the data the first NSUserDefault which is then presented in the data view. THe transition I am using is
[self.navigationController popToRootViewControllerAnimated:YES];
This transitions back to the data view successfully however it does not change the data in the view. However,when I remove that code and press the row and then press back, the readout is changed. How do I make the readout change on press of the row?
Try to use delegates/protocol,
Set your dataview as delegate to the protocol in history view. Before calling pop, call the delegate action. Then in the action, just refresh ur view in dataview and then call poptoroot.
Just google protocol/delegate example, there should be many.
Delegates / Protocols. The answers are abundant here for passing data between view controllers.
passing-data-between-view-controllers
All that I ended up having to do was implement
[self.dataTable reloadData]

Resources