Getting a UIButton to send commands to another View Controller - iphone

Question..
I'm currently making a conversion app. I have for the first tab where the information is entered. Views 2 and 3 are where the information from view 1. I'm having an issue.. I'm not sure how to send the information from view 1 to view 2 and 3.
I've looked at examples.. but I'm still not quite grasping the idea of it. any suggestions?
Thanks!

Instead of thinking in terms of sending information between views, create a "model object" that contains all the data that is shared between views, and let controller classes take care of updating the views when the model is changed.
Also, take a look at the documentation for NSNotificationCenter for a way to "broadcast" data updates throughout your app.

Related

Re-initialising a view after the save button has been touched

I have a TabBar Application with 2 tabs saving/fetching data to and from CoreData. The problem that I am having is that when the form has been filled and the user has touched the save button the view is not re-loaded or re-initialised. All I want is for the view to be ready for the user to repeat the process with the next set of information. I am probably not thinking about this in the correct way so a pointer in the right direction would be very much appreciated...
Do I need to manually set everything including the managedObjectContext etc. to nil? Or is there something that I can do with methods like viewWillDisappear that will elegantly help me to "re-initialise" that specific tab?
I ave read up the Apple docs on view hierarchies, and life cycles but I just seem to have confused myself...
Thanks in advance for any suggestions, referrals to code or even recommendations on relevant reading material.
It sounds like what you want to do is reload any data to their default states when the user taps a button. Unfortunately, you'll have to do this manually, setting each IBOutlet's value to a meaningful default (probably the empty string).
There are two ways I can think of that would help make this more elegant:
Use an IBOutletCollection and fast enumeration to loop over all the IBOutlets and not have a bunch of code to do each one individually.
If you're switching tabs in between these events, you can something neat and use your app delegate as your UITabBarControllerDelegate for the tabBarController:didSelectViewController: to call the clearing-out method for you instead of relying on viewDidAppear.

IPhone : What is the best practice to create application which shows 50 objective questions one by one

IPhone : What is the best practice to create application which shows 50 objective questions one by one. End of the test user should be able to see the score on the UI.
For storing the questions am using SQLLite DB, stored all my questions in a table and retrieved/stored locally in to NSMutable Array.
Now I have no idea how to navigate between questions "One By One" I guess UI navigation is one I want but Am puzzled how to use UI Navigation view to show question one by one , please correct me if am wrong.
The easiest way is probably UINavigationController with a UIViewController for each question. The problem is that you have 50 elements to be shown and probably that is too much (Because you surely get at least memory warnings)
In order to avoid memory warnings and to do this efficiently you want to implement a 3-views stack-like structure. For instance, views will be A,B and C.
...C←→A←→B←→C←→A...
For example you an in B, then you want to navigate to the previous(left) question you will save current question state, load in A and its state the previous view and show it. Now next and previous questions will be loaded in B and C views respectively. Not complicated, right?
You can simulate this structure with a UINavigationController and 3 UIViewControllers,
each UIViewController should be able to load and save a question with its state just before appearing and disappearing.
You can navigate programmatically using the following methods from UINavigationController and get animated transitions for free ;)
– pushViewController:animated:
– popViewControllerAnimated:
Hope it helps
I recently created an App like this for a client. My strategy was to have an IntroViewController, QuestionViewController, and ScoreViewController.
The QuestionViewController housed a UIControl subclass I created to visualize a Question object. The QuestionViewController interacts with a TestManager object to pull the list of questions, it responds to the input from the UIControl subclass, then assign a new question to the UIControl.
My strategy keeps memory low by avoiding creating a huge stack of view controllers. I utilized CATransitions to give the user a sense of new screens for each question.

Objective C Callbacks and Notifications

I'm new to Objective-C and not a full time programmer. I'm beginning to understand the Model-View-Controller design pattern for differentiating the UI from the model. So the user takes an action and the view controller sends a message to the delegate (model). But I'm not sure what the best way to send actions from the delegate back to the view controller.
For example, the user pushes a button, the VC messages the Delegate. That part I understand. Then the delegate takes action, and following that the delegate wants to update the VC (e.g., update a label).
So what I missed (or have forgotten) is how this gets done, while maintaining separation between the UI and the model. I suppose I can use the notification center. Or I think I can just have the view controller pass a callback to the delegate. Or maybe there's another choice I don't know of. Can someone give me a recommendation, please?
I think you're slightly misunderstanding the MVC paradigm. Models should never be delegates of views, since models should have no dependencies or knowledge of any view classes. Typically, a view sends a message to its delegate or target (if you're using target/action), which is usually a controller (often a subclass of UIViewController on iOS). The controller then accesses data from the model and can update any views that need updating. I'd recommend reading the MVC fundamentals guide for a more complete explanation.
Basically you're right, you could do all the notification-related things yourself (i.e. with NotificationCenter) but since we're talking about UI-Stuff here I would greatly recommend you to use IBAction-Methods and IBOutlet-Properties in your code which you can easily connect to UI-Elements respectively their Callbacks in Interface Builder.
A very basic introduction to this topic can be found here:
iPhone SDK Interface Builder basic training
i hope that it is not too basic tough, and that I could lead you on the right track.
First of all delegate is NOT a Model.
Model is something passive that only holds the data (DB, plist, array, dictionary etc.).
While delegate is some set of functions that exist in order to react to some events.
Delegate is more likely to be a view controller in your case.
The view controller should react to user's action.
If the button tap should display some data from your model in some label then view controller should do all the work (receive user's action, take the necessary data from the model and display it on the view...).

Passing Non-Static Objects Between View Controllers

I'm a newb to iphone development and objective c, but hoping some folks smarter than me can lend a hand. Here's my problem:
I have a view based app with about 7 different view controllers. The user navigates via a bottom tab bar. I have the users entering data in the first view controller to an object named "copies". I need to get the copies value to another controller so it can be used for calculations. This needs to be done for many objects in the apps other controllers too.
Example:
User enters Copies value in 1st view controller.
User enters Price value in 6th view controller.
7th view controller calculates copies x price = grand total
In my research I worked out the singleton method, but that seems limited to static data.
What's the best way to ensure that another view controller can access an object the user has filled in? I'm trying to avoid going a SQLite route currently. I want to stick to something basic and work my way up in complexity. Does anyone have any sample code I can review? It really helps to see how others have tackled this before.
Thanks in advance!
If I've understood you correctly there is just one copies value and one price value in the whole app. If that is the case...
There are many ways to do this. Easiest way (perhaps): you could make a Singleton object of a class that you define that has copies and price as properties. Singleton in Objective-C is here, and you would define your properties within the Singleton class. Then you would just call its shared instance and use the values on that. So your code would look like this:
[ThatCrazySingleton sharedInstance].copies = 5;
for writing.
Hope this is what you're looking for.
If you don't want to use a Singleton, at some point one of the UIViewControllers would need to send a message to the others with the copies and price values (hopefully wrapped up ["encapsulated"] in an object). This means that you have to get a reference to the other View controllers, which you can always do by going through the hierarchy.

How best to structure my code for a basic iPhone table/navigation database app?

Having worked through some tutorials on some basics via the iPhone, I'm struggling to determine how best to structure my code. The book I'm using points out things like "you wouldn't normally put this here, but for expediency...". Well, I'd like to know what one would "normally" do.
My application is somewhat simple - there is a table view showing a list of objects and one can add, remove, edit these objects (I plan to provide a more sophisticated organizational scheme later, but I'm keeping it simple to get something working).
So, I have a RootViewController that extends UITableViewController. When the "add" button is clicked I push a subclass of UIViewController onto the stack (this class is the "add/edit" form for my objects). I have a simple data structure-style class to hold the fields of the objects.
The apps like this in the book basically put an array inside the RootViewController and use a reference to the model class to represent the "object being edited". Basically, the models are all wrapped up in the view controllers. This seems wrong.
So, my question is: where do the models and the objects for managing them normally live?
And, does the answer to this depend on how I'm storing my objects? I have not done much with CoreData, though my plan was to use it for persistence of my objects. Will the hooks and boilerplate provided by XCode make this a nonissue?
Best answers will be pointers to some best practices type stuff, which I wasn't able to easily find via Google or on Apple's Dev site.
First of all you are right about your intuition that it seems wrong. As you described the model is stored in the view controllers. That is a bad idea. By doing so you are violating the model-view-controller paradigm, which makes your code hard to maintain.
So what you need to do now is get your model in a separate object or tree of objects or even better use CoreData, which is also great in terms of memory management.
As you want to use CoreData you should have a look at the UIFetchedResultsController class which you will use to obtain the objects from the managedObjectContext which will be your model.
What you would do in your table view - detail edit example is:
Fetch the contents of the table view using a fetch request and setting it on the NSFetchedResultsController you hold as an instance variable in the rootViewController
Set this rootViewController as the delegate of that NSFetchedResultsController
If an item is checked or the add button is pressed push your detail view controller on the stack, pass the object to be edited with it, or nil if it is a new object. Also pass the managedObjectContext to the detailViewController. Update or create the object.
Implement the delegate methods of NSFetchedResultsController in your rootViewController and there you reload the contents of the table when necessary.
What you gain is a nice and clear separation of model (CoreData's managedObjectContext) the controller (rootViewController and detailViewController) and you views. If you now edit an entry by using the detail view, your rootViewController is notified via your NSFetchedResultsController and automatically updated. What you also gain is that you do not have a strong reference among the viewControllers in your application.
Btw, you set up your CoreData stuff in the application's delegate. There is a lot of boilerplate code around in Xcode and on the ADC. Check out the Recipies app [1] in which this approach I just descriped is used. There are also some videos about CoreData on Apple's developers site.
[1]: http://developer.apple.com/iphone/library/samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html CoreDataRecipies