Bhavin Ramani Bhavin Ramani - 5 months ago 19
Objective-C Question

how to store button and textfield value when come back from other view

I have three view in my app. In first view there are three

button
and two
textfields
. When I press in any
button
there is a drop down list of
tableview
chosen item from
tableview
will be button title and in
textfield
the value will be from keyboard.

When I jump into second view to do my stuffs and come back to first view then the button title which I selected from
tableview
is not appear on
button
title as well as in
textfields
.

So,what to do to set
button
title and
textfield
value. I don't want to pass value from one view to second view.

Answer

Use NSUserdefaults to store the value of button & textfield

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

 // Store the data
 [[NSUserDefaults standardUserDefaults] setObject:yourTextfieldName.Text forKey:@"txtNmae"];
[[NSUserDefaults standardUserDefaults] setObject:cell.yourbuttonName.titleLabel.text forKey:@"buttonName"];
 [[NSUserDefaults standardUserDefaults]synchronize];

}

get the data from NSuserdefaults as

// Get the stored data before the view loads

 -(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];



 NSString * txtName = [[NSUserDefaults standardUserDefaults] objectForKey:@"txtNmae"];
if(txtName)
{
 yourtextField.text= txtName;
 }
else
 {
  yourtextField.text= @"";
 }

  NSString * buttonName = [[NSUserDefaults standardUserDefaults] objectForKey:@"buttonName"];
 if(buttonName)
 {
[yourbuttonName setTitle: buttonName forState:UIControlStateNormal];
 }
else
 {
  [yourbuttonName setTitle: @"as your Wish" forState:UIControlStateNormal];
 }


 // remove the userDefaultValue

 [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"buttonName"];
 [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"txtNmae"];

 [[NSUserDefaults standardUserDefaults]synchronize];

}
Comments