Executing Event on Unwind Segue - ios

I'm not sure if I explained this correctly in my question, so I'll explain it here. Please excuse my iOS illiteracy, complete newbie here.
The master view controller is a UITableViewController that holds a list of items. So, each cell contains an item. This list is populated from a Core Data model.
If I want to add an item, there's an Add button that takes me to a different view controller that has a text field. I write the item name in the text field, press the Done button and it adds it to Core Data, and unwinds back to the UITableViewController.
I want the table view to reload its data as soon as the unwind happens, so the user sees that his item has been entered into the list. I have to use a refresh control in order for the table to reload its data.
Is there a method whose code runs once the part written in bold happens?
Thanks in advance.

You can reload your table view data in the viewWillAppear method of the UITableViewController, which is called every time your view appears on screen. You can see this question for more details on the view controller lifecycle.

Related

how to program a button from main view controller to open another view controller - firestore records

I'm building my first app using Swift and have done a few modules from the CodeWithChris website. I'm just doing this as a hobby.
The main ViewController has a tableView with a list of records that are retrieved from FireStore. If you click one of the records, it will open another ViewController through a prepare for segue function and brings up a detail screen with various fields from that record for editing.
So back to the main view: In the tableView prototype cell, I have a horizontal stack view in which has two of the fields from the record and then I've added a button which I'd like to open a separate View different from the first I've described above but similar - so that it will bring up a detailed record view but with an entirely different set of fields that the other.
The concept is I'm tracking animal health histories so it shows a list of the animal names and types and when you click on the name or type, it goes to that first view with details about that animal. Then from that main tableview, I have a button that I want to bring up the vaccination history. I tried to do a prepare for segue by dragging the button to a new view controller but that didn't seem to work.
I'm just looking for the general idea of what I have to do and can probably use that feedback to maybe figure it out.
I figured it out. The segue identifier lets me set up some conditions within the prepare for segue function to active my goal!

UITableView display and reload behavior is out of sync

I've looked at other UITableView questions, but did not find any that matches this scenario.
I have a normal UITableViewController that accepts an array from a search form (IOW, dynamic data). The form has a push segue to the table VC, and the table cells have another push segue to a 'details' type of screen for each item. In the table view, I call reloadData in viewDidAppear.
The first time I test the search, the table displays the "no results" view (in a screen-wide cell) that I programmed into it, but only for about 1 second. After that, the search results appear as expected and the navigation works fine.
How can I get rid of this initial glitch?
EDIT: (#Paulw11's request) The search operation is network-based, and I call performSegueWithIdentifier in the dispatch_get_main_queue block. The data is passed in prepareForSegue as an NSArray (with multiple items per index).
This was actually an issue with the segue from the search controller to the table firing twice.
In my case, this was caused by the segue being wired to the search button instead of to the search view controller itself.

Refreshing a UITableView When Collection is Updated on Other View

It is hard to describe my scenario but I will try. I have a UITableViewController which allows the user to see the details of an item. After the user visit the item details controller they open another controller and then add more items. I want to refresh my original UITableViewController once the items are added.
I can of course use the viewDidAppear event but I have noticed that it slows things. viewDidLoad is only fired once.
Any other ways to update the UITableView remotely!
You can use view will appear instead if view did appear.

How to edit a UICollectionView item in another UIViewController?

I'm using a Master Detail View Controller with Swift.
When I click on a master cell, I see a collection view containing a list of pictures.
Each picture represent data stored inside Core Data.
Each picture data has also a timestamp and all picture taken the same day are stored in the same Master cell.
[Date of the day 1]
[pic1]
[pic2]
[Date of the day 2]
[pic3]
[pic4]
What I want to achieve is:
When I click on a picture, another ViewController is displayed (through a segue) and this new VC allows me to edit the picture and change its date.
The thing is I have multiple things happening here when I change the day or a picture:
if the pic was alone in the collection, then I also have to remove the Master Day cell too.
if the pic was not alone, I just need to update the detail collection view
In any cases, if I have to create a new Day, I need to create a new Master Cell and update the view and if I need to delete a Day, I have to also update the Master view.
If you edit the collection items, you can simply do a:
self.collectionView performBatchUpdates:
and then update the model, and notify the collection view of your action...
but I don't see how I can do that from another UIViewController.
Currently, in the Edit View Controller, I update the model and then dismiss the View controller. And I get a crash because the collectionView tries to display an Item that is not part of the current collection anymore...
To solve this issue, maybe I can use a protocol in order to inform the detail view from the edit view but If I do that, I need to give the IndexPath of the Edit Cell to the edit view and then give it back to the detail view through the protocol... Why not. but not that clean in my opinion.
Another problem is, the app seems to also crash in the master view when I delete a Day. Because, the master view doesn't know that I deleted a day from the edit view.
My main problem is that I don't see how to tell my Master View Controller that I made some model changes... especially when I don't have any IndexPath information from within the edit view.
Technically, I could use the notificationCenter to notify both Views, I guess... but still, I would need to pass both IndexPath from the Master view to the Detail view and then to the Edit view.
Ugly.
Several ways by which this can be achieved:
1) Delegation: The view controller containing collection view will act as a delegate of Edit View Controller and when editing is done it will callback a method to 'The view controller containing collection view'
2) Notification: EditingFinished
3) blocks
Using protocol is good solution but can be painful since you have to manage edited items using their indexPaths. A simpler solution, since you are using CoreData, is to fetch entries using a computed property and just call collectionView.reloadData() on viewWillAppear method from the controller that contains collectionView. This way calling reload data will recompute Core Data entries and will place each item in correct position. Also viewWillAppear will get called after dismissing controller that stands on top of collection view controller
I found an interesting article addressing my problem.
It was not indeed a trivial problem...
http://www.objc.io/issues/4-core-data/full-core-data-application/#using-a-collection-view

Populating a Text Field with a Selected Cell from a Separate Table View Controller

I have a working app which just needs some modification. It's a basic app with a Table View Controller that gets populated when a user taps the Plus button and fills in some information into text fields. The user will enter a name and event and I've made life easier by creating table view cells under the text fields so that when a user starts typing, it auto populates with the same names the user has entered before.
I'm using Core Data and NSFetchedResultsController. I'm modifying the app to be more seamless now for the user. Instead of the keyboard popping up when selecting the text field, I'm taking the user modally to another Table View Controller where the user can search, create or just select an existing entry from the Table View Cells.
I've got the new table view controller appearing and displaying the existing entries using NSFetchedResultsController and that's working well.
My question is: how do I go about selecting a Cell in the new Table View Controller and having that selection of the cell do two things:
1) Dismiss the Modal View
2) Populate the name text field (in the view controller that brought up the new Table View) with the selected name from the table view controller (that came up modally).
I have this working if the table view is in the same view controller, but I'm not quite sure how I would go about extracting that information from the other table view controller.
I'm guessing I would use protocols but I'm quite a newbie and so any pointing in the right direction or even some simple sample code would be massively appreciated!
Thanks,
You have to implement delegate protocol.
The idea is VC1 should conform the delegate (i.e. need to have a procedure -(void)dataAvail:(NSString *)data withViewController:(ViewController *)sender{...}). VC1 will push VC2 and set VC2.delegate = self.
In VC2, when cell is selected, just call [self.delegate dataAvail:self.yourTextField.text withViewController:self];
Now, in your VC1, you should implement dataAvail... just get the value and dismiss your modal VC2.
For more info on how to declare the protocol, just google for "ios delegate tutorial". delegate Protocol is used everywhere when coding for iOS (like MVC or KVC).

Resources