Performing a FetchRequest from another view controller - ios

In my new Swift app I am using a slide side menu with SWRevealViewcontroller.
On the rear view controller there are several fetch requests to retrieve the number of core data objects that meet some conditions. On the front view controller, the app user can create new core data objects.
In the normal way of using the app, the app user creates or modified the core data objects, and later he/she can reveal the rear view controller to see the number of objects of each type.
Please take a look of the two view controllers:
Front view controller:
If the user taps on the +button, a new object is created.
if the user taps on the menu button, the rear view controller is shown, as you may see in the image:
And now, finally my question: As you may see in the image, both view controllers are loaded and showed. I want to know if it is possible that on this scenario, with both view controllers on screen, if the +button is tapped on the right view controller, could I implement a way to perform the fetch requests on the left view controller to update the count of the objects of each type?

I have solved it using a NSTimer on the rear view controller. I guess this is not the best solution, but it works. If the user taps on the +button to create a new object, the timer launches the fetch request to update the number of objects from each type.

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!

Pass data between viewcontroller without changing views

As the title states, I'm looking to simply pass the information.
I have a tabbed view application currently, and the user inputs data into a text field, presses a button, then labels are filled with the entered text on the same viewcontroller.
I want to send that information to the other tab and fill a label.
I know I can do this via protocol or segues, however, I want to remain on the current tab. I haven't seen this as an example anywhere, only to switch the view to the other screen.
Anyone know how to simply pass the string entered and not change the view?
You are looking at this all wrong. You do not want or need to pass data between view controllers. If your app makes proper use of MVC (model, view, controller), then what you should be doing is updating a model. That model should broadcast that is has been updated. Anyone that cares about the model should react to those notifications as needed.
You have a tab controller with multiple view controllers. Two or more of your view controllers have an interest in the same data model. Both should reference the same instance of the data and be setup to be notified about changes to that instance of the data model.
One view controller, through its views, updates the data model. The data model then sends out a notification that is has been updated. Now the interested view controllers receive this notification and update their own views based on the updated data model.
No view transitions required. No segues required. No communication between different view controllers required.
Look at the documentation for NotificationCenter for ways to broadcast messages and for ways to listen for such messages.

How to create simple tableview and load data using Swift in iOS

I am trying to create an app for iOS using Swift but running into problems with the very basics.
To keep it simple I just want the app to initially be a single view application with a button and some sort of list view on the page. I believe a TableView is what is recommended here. When I click the button, I just want it to populate the list/table view with some entries, that's it. To start with, I don't care if these entries are hard-coded, I just want to get something working.
I have been looking at different samples but I am getting confused. Some of them seem to suggest using a TableViewController others don't. When I use a tableview controller, the UI I had created seems to get completely replaced with just an empty tableview list and the button is gone.
I previously have developed apps in Windows phone and found it a lot easier. I'd just add a listview object and in the click method of button, add the items programmatically etc. But this is my first time trying to create an iOS app and it seems a lot more confusing. There are delegates, controllers, views all seemingly needed in order to do something very simple.
Can anyone give me some basic step by step instructions about how to add a tableview to an application and load some data into it through a button click?
Make sure you are clear about the difference between a view and a view controller.
iOS uses the MVC design pattern (Model View Controller).
A view object displays contents to the user and responds to user interaction.
A model object stores state data.
A controller object drives the app logic and mediates between the model and the view.
A UITableViewController is a special subclass of a UIViewController who's job is to manage a table view. It has some extra support in it that makes it a good choice for managing a table view, BUT... there is one annoying thing about it. It is designed so the ONLY view it can manage is a table view. You can't use a UITableViewController if you want to add buttons, labels, and other UI elements to your screen outside of the table view.
What I usually do is to create a create a table view controller, create a separate regular view controller, add a container view to the regular view controller, and then use an embed segue to embed the table view controller inside the view controller. (you just control-drag from the container view to the table view controller.) That way you get the best of both worlds. You may want to create a protocol that the table view controller would use to communicate with it's parent view controller.
You should be able to find a tutorial online on setting up a table view controller as a child of another view controller using container views and embed segues. It's quite easy.

iPhone app, how to handle the back button with dynamic content

I have a tab bar controller view that loads a record from an api call. Since different data is shown for the same record on each tab I'm storing the object in the app delegate and just grabbing it on viewDidLoad.
The problem I'm running into is that this record is related to other records, and one of the tabs has the other records listed. So if you click on one of those it needs to essentially reload the same tab bar controller.
I'm going to reload the same controller using the instantiateViewControllerWithIdentifer method, but how would the back button work on this since if it went back that object is now gone.
My thoughts on reloading old objects was to keep an array of all the ids of the records loaded, and just go back to the last id and reload the object from the api.
For what you are trying to do, I would suggest creating your own custom implementation of a tab bar controller. By doing this you will be able store the data from each record as a property in your custom tabbar controller. You can then have multiple instances of your tab bar controller, each with its own data (and you won't have to store anything in your app delegate, which ideally shouldn't be used for this purpose anyway.)
Here is an example tutorial in which the author creates a custom tab bar controller similar to what you would need:
http://www.iphonedevcentral.com/create-uitabbarcontroller/

TableView index from rootcontroller to detailcontroller (UINavigationController)

My root view is a grouped tableview. When the user selects a row, i want to be able to retain which row was selected to give to the detailcontroller for loading the correct data.
All the navigation is working fine, I just don't want to have to create a global variable to just retain the index. Is there a built in method for the navcontroller or something?
You can either set a property in you detail view controller before it is pushed giving info about the row selected so it can influence the behaviour of the detail view controller, or it is possible to access the parent view controller directly from the detail view controller with:
#property(nonatomic, readonly) UIViewController *parentViewController
I prefer the first option - you can write a custom init... method, to supply the data for the detail view controller when it's created.
There isn't a builtin way as such, you should try and make the view controllers de-coupled as possible so they can be re-used and are resistent to changes elsewhere in the app.
This is a useful quote from the View Controller Programming Guide for iOS:
With the exception of view controllers managing leaf data, each custom view controller must provide a way for the user to navigate to the next level of the data hierarchy. A view controller that displays a list of items can use taps in a given table cell to display the next level of data. For example, when a user selects a photo album from the top-level list, the Photos application creates a new photo album view controller. The new view controller is initialized with enough information about the album for it to present the relevant photos.
I wanted to follow up with how I ended up coding this. I'm still somewhat new to Obj-C, so some concepts I haven't dealt with or come across, so if this is old hat, sorry.
You can create your data element in the child controller and pass the data 1 for 1 directly to that element by accessing its property.
ParentController.h
NSDictionary *myData;
ChildController.h
NSDictionary *childData;
// This would get called in the parent controller where appropriate
// but before the child controller is presented
ChildController.childData = myData;
This is obviously very stripped down, but the idea works. Just take your data and pass it to the property before calling it and it will be there once the child view is presented. Hope this helps.

Resources