Editing text in UITableView in second view controller
I have a static table view (FirstViewController), with 3 rows, each in their own section. the first two cells have UITextFields in them, which are editable when the user taps on them or the cell. The last cell has a UILabel, which when tapped pushes SecondViewController, which contains a UITextField. When the user presses back the value of the UILabel needs to be the value of the UITextField.
If I create a delegate property (assign) on the SecondViewController, which is set to FirstViewController, what guarantee is there that FirstViewController will still be in memory and not nill? As I understand it as soon a view controller is not the top most view controller (the visible one) it can be deallocated. So what would happen if the device runs out of memory, and deallocates the FirstVie开发者_StackOverflow中文版wController, then when the user presses back the return method will not be sent as delegate
will be nil, and after that a new instance of FirstViewController will be created and popped onto the screen, without receiving the value from the SecondViewController.
I don't want to use a "global" variable in the AppDelegate, as I personally think that's a bit messy.
You can create a Data class where you can set the properties of variables or arrays (for displaying data in UITableView). Implement a class method in data class which checks that object has been instantiated or not. If not, it does that. It is something like this :
//DataClass.h
@interface DataClass : NSObject {
NSMutableArray *nameArray;
NSMutableArray *placeArray;
}
@property(nonatomic,retain)NSMutableArray *nameArray;
@property(nonatomic,retain)NSMutableArray *placeArray;
+(DataClass*)getInstance;
@end
//DataClass.m
@implementation DataClass
@synthesize nameArray;
@synthesize placeArray;
static DataClass *instance =nil;
+(DataClass *)getInstance
{
@synchronized(self)
{
if(instance==nil)
{
instance= [DataClass new];
}
}
return instance;
}
Now in your view controller you need to call this method as :
DataClass *obj=[DataClass getInstance];
And use the arrays.
This way you can assign data without disturbing AppDelegate
, which is a good practice.
精彩评论