Is this a good architect for my Iphone app? - iphone

I am trying to learn iphone programming so please be patient.
I have a simple application implemented using a root model and two views selectable by a Tab bar control. The user enters a value in the view A and pressed a button. The appropriate IBAction of the button sets a property in the root model and then it calls a method(DoCalculation) inside root model to perform some calculations. The result of calculations are a series of numeric data which will be saved in a NSMUTuableArray. This array is used to update the information in view B.
My questions are: Is this a reasonable architecture of this simple app? Do you see any problem with it?
Another question is should I update view B right after the calculations are done in Root model or should I store then in an arrray and the view should update itself when it becomes visible?

That's one of many ways it could be done. Retained data can be stored in individual instance variables or in a structure such as an NSMutableDictionary or NSMutableArray.
Generally you don't update the view until it's about to display (eg, in viewWillAppear), since you have to wait until the view is created anyway, before it can be updated. (You can force creation early by referencing the view property of the view controller, but it's generally better to wait for it to be created by the flow of the app, since a created view occupies a lot of storage.)

Related

retain entries in UITableView

I have the following (view) controller arrangement:
an uitableview --(embedded in)--> navigation controller --(embedded in)--> Tab bar controller --(modal segue to)--> button in a view (uiviewcontroller).
description of my problem:
The view with the button is the initial view of my app. Pressing this button leads to a tableview in a tab bar. At the beginning the table view is empty, but you can manually add information on the cells, like names. However, once you go back to initial view (with button) and press the button again, the previous entries in the table view are erased.Can somebody give a hint how to retain these entries with the given arrangement. Thank you very much in advance.
You can use the singleton pattern. It will create a shared instance of your object, that you can access anywhere. You will then be able to write in it when the user adds a row in the table, and read it when you come to the tableView.
Here is a bit of help : Singleton pattern
EDIT : you can also use core data, but it may not be appropriate in your situation (plenty of docs on the web about that one)
This is quite a question as it really isn't about the interface per se. It is more to do with general Object Orientated Programming and the life time of objects.
Basically your data needs to be stored higher up the object hierarchy. It needs to persist across construction/destruction of the interface objects. This could also be done by storing the data in persist-able data store such as a DB or file but you would still need to store the means to extract the data or subset of data from the store.
If you wish to persist the data you could store data in a singleton data class and reference it statically or store it in your UIApplication object and pass that down the hierarchy as you create each object. you would probably have to subclass each object and add a new constructor passing in a ref to the data.

XCODE : Using TabBar to view two same ViewControllers that show different filters on the datasource

I have a problem with my App for iPhone.
Enviroment: I have a TableView, that operates above an MutableArray datasource. An example would be Lap Times for Go cart pilots.
Datasource: I record lap times from both training sessions and from race and qualifying. I consider race + qualifying as one subset of data and training sessions as another subset of data because I think there is big psychological effect and my drivers have slower laps when racing because of fear of crashing into others. At this time I record both data in one array with a variable of Bool "fromRace". So I can filter racing and out of race times.
Human Interface: I created a TabBar that shows racing times in first Tab and training times in second Tab. It feels, like those Tabs contain basically the same view and operate on same data which I just filter using the same view controller since all the methods are the same.
Is it programatically correct to create two separate view controllers and two separate arrays for the datasource basically duplicating the code? Or try somehow to try to discover from which tab I came and alter the data presentation accordingly using just one view controller?
I hope I am clear on what I ask for.
Yes - broadly speaking, a UITabBar is used exclusively to switch between view controllers. That's how it's designed to operate - you load each 'slot' in the tab-bar up with a view controller.
That doesn't mean you have to create two separate view controllers - you could create two instances of the same view controller, and have some flag passed in upon initialisation that would display the data in a different way.
If you just wanted to have a single view controller adapt to display the data in different ways a UIToolBar would be a more appropriate UI element (perhaps with a segmented controller at the bottom).

iPhone programming guideline : List / detail / modify

I have a program that displays a list (a TableView).
When the user clicks an item, it's detail is shown.
On the detail view, the user can ask to modify it so a modify window is shown.
Here, the user can ask to delete the item.
I would like at this time return to the list with the item deleted from the list and from the data source.
There may be thousands of methods to do this, but I wonder which is the best / good one.
Could you help me, and/or give me a good reference to read about this ?
I think he ask how to get from view number 3 back to first view.
Then you can put your three views inside a UINavigationController and call on the last view popToRootViewControllerAnimated:
at least Two options:
Delegation - Create a protocol called something like: TableDetailModifierDelegat
and add methods like modifierDidChangedItem:(id)item
or modifierDidDeleteItem:(id)item
and to the modifying view controller add instance variable id so when you done editing or deleting you will call the appropriate methods.
of course you will need to make your table view controller implement the protocol you created. if you modified or deleted an item you should update your data source and reload the data to the table.
also pass the Table View Controller as the delegate when creating the Modifying View Controller.
Passing The Data Model.
This is much more simple to implement.
you can simply pass your data model to the modifying view controller, and make the changes directly to the data model.
You should keep the data model as Instance Variable in the modifying view controller.
when you done, dont forget to reload the data to your table.
In this instance, you could simply remove the data from the data source the UITableView is using and then call the reloadData method on the table. (As you're editing the data in question, you'll presumably have a suitable method/reference by which to delete it.)
As a bit of reading, I'd recommend Apple's Table View Programming Guide for iOS - this provides a lot of useful background information.

How to Handle an Indefinite Amount of TableViews in an iPhone drill-down app

I've Created a UITableViewController subclass. Do I only need one controller? Do I just instantiate multiple instances of that one subclass?
The iPhone App I'm building will start with a Table of a list of people. If the user taps a person, a new table will be pushed in with a list of Companies they've worked for. If the user then taps a company, they'll see a list of Job Positions. And if they tap a position they'll see a list of people holding those positions.
This could go on forever, and the user could always back up the list.
The App Delegate instantiates the Navigation Controller and the Table View Controller and then pushes it onto the Navigation Controller. But when a user taps a row, now the TVC is creating another TVC.
Is that right or should the
AppDelegate be instantiating all
TVC's? Or does it work out since
they all get pushed onto the Nav
Controller anyway?
Does each Table View instance
need to have a different name or can
they all be called 'mainTVC' or
something like that?
tableViewController *mainTVC = [[tableViewController alloc] init];
Won't I run out of memory? Do i
need to start dropping Table Views
when they're 2 or 3 levels away from
current, and then re-create it if
the user backs up to it?
No need to create multiple TableView's, what I've done in the past is simply re-bind to a different datasource. So keep one TableView and have a datasource for people, then companies, etc...
I'd create a view controller for each type. Presumably you'll want to have special display characteristics like a custom tableview cell to display job positions slightly differently then you would people names.
Other then that, #Ben Gottlieb's answer should work quite well. Use lots of view controllers and handle the didReceiveMemoryWarning: method.
One more thing, if the user drills down so far that you want to say they'll never go all the way back (sort of like having an undo stack) you can use the setViewControllers:animated: UINavigationController method to reset the stack to a certain size (say 15 to implement an 'undo buffer' of 15). With this method you can make sure that the first view controller is always your root view controller and the rest are all drilldown instances.
Option number (2) looks good. You can push quite a lot of view controllers onto the stack before memory becomes an issue. The system will clean up most of the heavyweight memory hogs (ie, the views) in the didReceiveMemoryWarning: method. If you're creating a lot of in-memory structures, you may want to clean them up in that method (be sure to call the inherited method).
To answer your third question, as long as you don't have huge data stores in memory, memory shouldn't be an issue. You SHOULD NOT under any circumstances "drop" tableviews - this would lead to crashes(and there's no way to do non-FILO additions/removals to the navigation stack anyway). Under memory pressure, you should only free "nonessential" items like caches. However, this shouldn't be an issue.
Also, if you have more than 3 or so levels, chances are you need to rethink your UI. If users are drilling down 10 levels, it will be tedious to navigate the stack back.

Passing data from modal navigation controller to parent controller?

I'm starting out in iPhone development, and I'm trying to do a Contacts-style controller to add items to a table. I've got the two controllers designed, but I can't seem to figure out how to pass data between them. How can I get data from the modal controller to its parent?
Just trying to call methods on parentViewController won't work (and results in compiler warnings and crashes). I've considered moving the data out of the controller itself (which is going to happen anyway, I'm just trying to keep things as simple as possible for now), but even then I still have to somehow tell the parent view to refresh its data. Again, how can I do this?
I've considered moving the data out of the controller itself (which is going to happen anyway
I think now may be the time to follow that consideration and create a separate "ContactList" model object to store your data.
Your modal view controller just adds new "Contacts" into the "ContactList".
Your TableViewController should observe the same "ContactList". As items are added/removed to/from the "ContactList" your TableViewController will simply add and remove rows from its tableView.
In this way you will keep each ViewController independent, your code will be more flexible and adding/removing single rows will be much more efficient than reloading an entire tableView.
You either keep a link to the sub view you create and ask it for data that has changed on return, or else ad yourself as a delegate of a subview and have it call you back directly (which does work if you have a valid reference).
I had the same question and found this sample code: http://developer.apple.com/iphone/library/samplecode/LocateMe/Introduction/Intro.html
Helped me a ton and was pretty easy to figure out from the code.
In short, use delegate.
I would suggest you have a look at Lecture 11: Text input, Presenting content modally (May 6) - iPhone App Programming course offered by Stanford. Search for it on iTunes U.
Download this sample app if you want to know how to implement delegate: http://www.stanford.edu/class/cs193p/downloads/11-Pickers.zip