I'm going to explain my scenario: I have a custom class whose properties are intended to get the information provided by the user through a form. This is the custom class:
#interface CustomClass : NSObject
#property NSInteger iD;
#property (strong, nonatomic) NSString *profilePicUrl;
#property (strong, nonatomic) NSString *email;
#property (strong, nonatomic) NSString *firstName;
#property (strong, nonatomic) NSString *lastName;
#property (strong, nonatomic) NSString *address;
#property NSInteger zipCode;
#property (strong, nonatomic) NSMutableArray *products;
#property BOOL isEnabled;
#end
The form consist of three views with an UITableView, and their respective view controllers are within a UINavigationController. This way, it is like a multi-step form: there is a first view requesting user input, then navigate to a second view requesting user input, then navigate to the third view displaying information before submitting the data provided. The "back" button of the navigation bar is enabled, so users could go back to a previous step to complete/change their inputs.
Cells of the tables are of different types, corresponding to the properties of the custom class I described: some of them have a text field (cells for entering first name and last name, for example), some others a switch ("isEnabled" property), and I have one cell with a button that displays an UIImagePickerController to take a picture (cell for profile picture).
I want to create an instance of my CustomClass and have its data completed at third step of the form to submit it. A part of the information is requested in the first view (profile picture, email, first and last name), and the rest is requested in the second view (address, zip code, products), so I'd need to pass the CustomClass object through the three view controllers of the navigation hierarchy.
My question is: how should I manage the persistence of the values in cells when the user enters them? Should I "bind" somehow the CustomClass properties to each corresponding cell? I'd want the user to be able to go fordward and back in the form and keep the data that she already entered. It looks like the values I type in text fields are retained and if I navigate from first view to second, and I go back to first again, the text field values are kept, but not the rest of the cells' content. And what if the app suddenly terminates and I donĀ“t want the user to enter again all the information? Note: my app has to support iOS 5.
What should be the best way to manage this scenario? Thanks!
You have to persist the info in your CustomClass. You could do this by writing it to a plain text file, a property list or use a database, or even use the NSUserDefaults infrastructure.
The save should occur immediately whenever data has been entered, for example in textField:didEndEditingand such callbacks. You would update the property of your CustomClass object and then persist it.
Related
This might be a completely dumb question, but I'm a CD noob...
I have an object that tracks rectangular points on maps by defining the top left and bottom right corners, like this...
#property (nonatomic) float latitudeNorth; // same as "northwest"
#property (nonatomic) float latitudeSouth; // same as "southeast"
#property (nonatomic) float longitudeEast; // same as "southeast"
#property (nonatomic) float longitudeWest; // same as "northwest"
Now I need to add four more points, so that we can have polys instead of pure rectangles. So I want to add this...
#property (nonatomic) float latitudeNorthEast;
#property (nonatomic) float latitudeSouthWest;
#property (nonatomic) float longitudeNorthEast;
#property (nonatomic) float longitudeSouthWest;
These data points are ephemeral and exist only as long as the app is running. They were, however, originally built in the xcdatamodeld. Is this a problem? Or am I find just adding the additional properties and using the old xcdatamodeld as-is?
I depends on you complete structure.
If you have some more entities that are fetched several times and saved again, and you need every time you fetch also the ephemeral properties, you have to hold them of course in the DB.
But if you only need them while you fetched and holding the Object, you can handle them just in class and there is no need to put them into the DB.
Not every item in the DB also has to be in the extracted class ;)
But the most attributes will also be saved, because they have to be updated via other services i.e. or have to present these updates in other views. Or maybe also because you don't want to hold the attributes all the time or will fetch them again (i.e. after a tableView.reloadData) and don't want to lose the calculated results
You can have other properties declared in your managed object subclass. They won't be initialized when you load up your object from a persistent store (when you close your app and open it up again, the values will go away), but you can get around that. However, there are better ways to do this. For example, you can write methods that calculate the values on the spot:
-(float)latitudeNorthEast;
-(float)latitudeSouthWest;
-(float)longitudeNorthEast;
-(float)longitudeSouthWest;
In your case, all that these methods do is return one of the other property values, so this seems like the way to go.
I'm currently writing an iPhone app that needs to pass data from a text field on the Flipside view to the Main view when the user clicks the Done button. This is the last component of a project that I am working on.
I've tried including my MainViewController.h file on my FlipsideViewController.m file, and this doesn't work. I'm currently writing in the - (IBAction)done:(id)sender portion, something like
MainController._aTextFieldOnTheMainView.text = _aTextFieldOnTheFlipsideView.text;
but I always get this error: Property '_aTextFieldOnTheMainView' not found on object of type 'MainViewController'.
What am I doing wrong?
Change/remove your weak declaration of #property (weak, nonatomic) IBOutlet UILabel *aTextFieldOnTheMainView;. This causes the deallocation of the property when the flipsideview comes onto the screen.
hope this helps
I'm basically an Android programmer and on my way to learning some skills on iOS platform.
I have an Android application, which has a custom UI component which looks like
.
I want to create a similar re-usable UI component for my sample iOS application. I'm not able to get any lead on how to do this in iOS.
On Android I can create a composite UI element using a layout file but in iOS i'm not sure if i can create a composite UI element or extend UIView and then somehow layout Text and image components inside it.
I'm looking for some leads on implementing this. I plan to have a multiple instances of these component on screen & the values gets updated from a web service.
I recommend something called interface builder in iOS.
It is a place where you can visually place elements that the user interacts with and you can see how the design looks as you layout your structure.
For tutorials, you can look at http://mobile.tutsplus.com/tutorials/iphone/interface-builder/
or search up "ios xib tutorial"
Hope this helped!
If you want it to be a simple view, then you could create a UIView sub-class with a few UITextFields's and probably an UIImageView or two that all have outlets so that your controller can make changes to it. For instance:
#interface StockInfo <UIView>
#property (nonatomic, strong, readonly) UITextField *ticker;
// You may want to make these numbers so that you can do calculations with them, and then update the text field automatically
#property (nonatomic, strong, readonly) UITextField *price;
#property (nonatomic, strong, readonly) UITextField *priceChange;
// This could be automatically calculated based on the price and priceChange if appropriate
// It could also automatically show the Up or Down indicator
#property (nonatomic, strong, readonly) UITextField *percentChange;
#end
Then, your controller could create an instance and set the various properties:
StockInfo *djia = [[StockInfo alloc] init];
djia.ticker = #"DJIA";
djia.price = #"14550.35" ;
djia.priceChange = #"-111.66";
// ...
You can create the actual UI elements within the view either in Interface Builder, or do it in code. Which to do is kind of a personal preference. There are plusses and minuses to both, and building the view in code in this case would be pretty easy and not require you to have two files in order to use the control.
I am using one simple UIViewController for my application which is not too complicated. It has two pages. On the first page I enter data in to text boxes to indicate user changeable data which will be handled on the second page.
#property (weak, nonatomic) IBOutlet UITextField *routeText;
When I am running my actions on the second page, I use a command like this to access what was typed in to the text box:
NSString *variable = [[NSString alloc]initWithFormat:#"%#",self.routeLabel.text];
self.consoleView.text = variable;
But the value is always (null). I am using the same TravelViewController.h/.m for all of the code. My question is: Is this not acceptable coding behavior to share it in this way or do I need to define the objects in a different way so the text can be shared between views?
Thanks for any help you could provide.
If it is a different view controller you need to pass the text from parent view controller to child using #property. Declare it in second class and when you are pushing from first screen to second, set this value in #property. After that you can use it in second screen using self.text.
I'm new to Core Data so I thought I'd ask this here.
I have a model, User Recording, which, for now, has the following:
#property (nonatomic, retain) NSDate * dateCreated;
#property (nonatomic, retain) NSData * audioData;
#property (nonatomic, retain) NSString * name;
What I'd really like is to have a method in there, called "play", to play the recording. Right now, I'm putting it in my view controllers but that's clearly bad because I've got that same method in two controllers. I've looked around a bit (and will keep looking) but can't figure it out - where should it go? Should I have a model controller (manager)?
Thanks.
your controller should have a hold to the model (that is, an instance variable or property), so that when the user clicks a button in the view (which should have an outlet to connect to the controller) you can invoke the message you want.
Just add the play method to your model (in the header file, so that is's public) and let the controller call that method when the user interacts with the view.