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

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/

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!

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.

Change view controller cause reset changes

I have two view controller. On the Second controller I have a button which cause a UILabel to change from "larry" to the string "happy". If I press back, which take me to first view controller, and then forward, which take me to the second view controller (from the first view controller). I found that the UILabel change back to default, "larry". How do I make it so that when I change view controller, the changes and data doesn't get clear away.
Recap: How to make it so that every change in a view controller doesn't change back to default after changing to another view controller.
This doesn't only happen with UILable, it also happen with
self.loadPage.hidden = YES;
I also found out what I made to turn 'hidden' also appear back after I pressed back and forward.
Thank
The problem that you are facing is due to the fact that when you press the button a new instance of the second viewcontroller is created and that old one is destroyed.
If you want to persist the data while pressing the buttons, you need some way to store the information somewhere
there are some methods to got about it
You can use instance variables in both the classes to store the data temporarily and display them. You need to know the concept of protocols and delegates for this as well.
You can store the information in NSUserDefaults for some moment so that you can access it later on.
The issue is each time when you goes from first view controller to second, you are initialising a new instance of second view controller which brings back every values you changed to default values.
You need to save the values elsewhere and assign those values whenever control is switched from one view controller to another

Performing a FetchRequest from another view controller

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.

How can I refresh view every time when a tab bar item is clicked?

I have an UITabBarController(empty) which have 2 subView. These two subView are tableView(get data from database). I want to refresh view every time when a tab bar item is clicked(get new data from DB).
I search many method on Internet. Some said use the viewwillappear. But did work.
Can anyone give me some Suggestions? Thanks
You set something to be the tab bar controller delegate and when the user clicks on a tab bar item, override:
tabBarController:didSelectViewController:
and each time the item is touched, send a message to the view controller (which is one of the parameters that comes in via that delegate method) to refresh whatever you want.
Don't use "viewWillAppear". That's meant to be called right before the view is meant to appear.
I'd suggest implementing a custom "refreshView:" method (or you can choose whatever method name that makes sense to you) and inside there, do whatever UI refreshing you want to do for the view owned by that view controller.

Resources