-1

In my case there are two ViewControllers. In my first view controller there is a label and when view load, it's text should display as 1. then there is a button, when it click,navigate to second view controller. In the second view , there is a stepper and label. If user tap + ,second view's label text change from 1 to 9,for the - also same(decrease the value). in the second view also there is a button.when it click, second view dismiss (from the first view to second I used presend Modally kind segue with over Current Context presentation.that means when dismiss this secondview, firstview does not load again,it exists in the background). so what I want is to send the second view's label text (after changed by the stepper), as first view's text and update the first view's label.(think if the second view's label text is 3, first view's label text should update from 1 to 3 ). I tried with NSUserdefaults.this is my code.

this is my second view controller

- (void)viewDidLoad {
    [super viewDidLoad];


    //set default value for adult label
    NSUInteger defaultAdultVal = self.adultstepper.value;
    self.adultcountLabel.text = [NSString stringWithFormat:@"%d", defaultAdultVal];

}




- (IBAction)adultcountAction:(UIStepper *)sender {

    NSUInteger adultVal = sender.value;
    self.adultcountLabel.text = [NSString stringWithFormat:@"%d", adultVal];

    NSString *adultCount = [NSString stringWithFormat:@"%d", adultVal];
    [[NSUserDefaults standardUserDefaults] setObject:adultCount forKey:@"adultcount"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}
- (IBAction)DoneAction:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];
}

and this is my second view controller

- (NSString *)testingAsign
{
    NSString *adltCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"adultcount"];
    return adltCount;
}

I'm getting the value with this method in the first view, and I want to update first view's value but it didn't work.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
caldera.sac
  • 4,918
  • 7
  • 37
  • 69

5 Answers5

2

There are many ways to this are

  1. By using protocol-delegate - Perfect way

proper and perfect way it to create protocol for it in secondVC, and add one weak property as delegate, and while presenting secondVC.. assign firstVC as delegate of secondVC. Also, Implement that protocol in firstVC. Now when you are dismissing secondVC, call the method in protocol. And implemented method in firstVC get called.. so you get the value there.

  1. By using NSNotification

You can add observer for notification in firstVC and postNotification from secondVC. But this is not proper way.. as firstVC continuously observes for notification. (Don't forget to remove observer.. once you dont require observation)

  1. By using Global variable

You can add one global variable in appDelegate, and assign its value from secondVC. And access that value from firstVC. This is also not proper way. Because that variable always remain in memory.

Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
  • that is total wrong way.. for passing runtime variable values, we should never use memory.. i mean Saving in NSUserDefaults comes when you want to save even after killing the app. i mean if you want to keep detail in persistent memory.. otherwise for this purpose, it is totally wrong. – Mehul Thakkar Aug 17 '16 at 04:48
0

Trying adding value from userdefault in view will appear in first view controller when second view controller dismiss after setting value in NSUserDefaults.

- (void) viewDidLoad:(BOOL)animated{
   [super viewDidLoad:animated];
   [[NSUserDefaults standardUserDefaults] @"1" forKey:@"adultcount"];
   [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void) viewWillAppear:(BOOL)animated{
   [super viewWillAppear:animated];
   myStaticLabel.text = testingAsign;
}

- (NSString *)testingAsign
{
    NSString *adltCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"adultcount"];
    return adltCount;
}
Devanshu Saini
  • 765
  • 5
  • 24
  • this is not working.the thing is if I go to the `second view ` and when it `dismiss`, my `first view` does not loads again. – caldera.sac Jan 21 '16 at 07:18
  • I think that is because I use 'over current context' as `presentation` for the segue.If I change it to any other like 'currnent context ' it works properly – caldera.sac Jan 21 '16 at 07:26
0

this can be done with so many ways like using delegate methods coredata , nsnotifications and nsuserdefaults. from all these , one of the easiest way to pass data backward, we can easily use NSUserDefaults. this is a sample project of passing data backwark using nsuserdefaults. use this github project and give it a try. project url : pass data backward using NSUserDefaults in ios, objective C

caldera.sac
  • 4,918
  • 7
  • 37
  • 69
-1

You don't need NSUSerDefaults for this. If all you want to do is to be able to transfer data on segue, you need to use the prepareForSegue function. Check out https://stackoverflow.com/a/7865100/2465172

Community
  • 1
  • 1
-1

In which method are you updating in FirstViewController, ViewDidLoad or ViewWillAppear Method? Do [[NSUserDefaults standardUserDefaults] @"1" forKey:@"adultcount"];[[NSUserDefaults standardUserDefaults] synchronize]; in SecondViewController and when you dismiss SecondViewController,get updated value from NSUserDefaults in ViewWillAppear Method of FirstViewController.Hope it will work.