-2

Here's the situation. I have 3 view controllers named as A, B and C.

I'm navigating from A to B and passing some string , call it as receivedMember.

In B when I write NSLog(@"Name is %@", _receivedMember); // it prints the value that was passed from A

Then from B, I'm navigating to C and again returning to B. So when I returned to B, I checked the value of name. It is null.

  -(void) viewDidAppear:(BOOL)animated{

     NSLog(@"DID APPEAR %@\n", _receivedMember);   // (null)
    }

    -(void) viewWillAppear:(BOOL)animated
    {
       NSLog(@"WILL APPEAR %@\n", _receivedMember); // (null)
    }

EDIT Here is the code

In class A, A ---> B

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"searchpage"])
    {
        B *cl = segue.destinationViewController;

        cl.receivedMember = strMemberType;

    }
}

I'm getting the strMemberType from pickerview,

-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{

        strMemberType = [memberTypeArray objectAtIndex:row];

}

In class C, C ---> B

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"backToSearchSegue"])
    {
        B *cs = segue.destinationViewController;
    }
}

B.h

@property (nonatomic, copy) NSString *receivedMember;

How can I get the same value when I return to B from C.? I'm naive in iOS, so please help me out. Is there anyway I can store them in cache file? So that I can access it in any class.

Aniruddha
  • 4,477
  • 2
  • 21
  • 39

3 Answers3

2

in iOS the data passing method in used in several ways,

example 1.

AppDelegate

use this link How to pass data from AppDelegate to ViewController?

example 2:

NSObject class method

use this link Sending data from one ViewController to another.

example 3 :

using segue method.

use this link ios which is the best way to pass data from one viewcontroller to another . through singleton class or *.h file?

example 4 :

singleton class method

use this http://klanguedoc.hubpages.com/hub/iOS-5-How-To-Share-Data-Between-View-Controllers-using-a-Singleton

example 5 :

NSUserDefaults

use this link : Pass data from one ViewController to another ViewController using NSUserdefaults

Community
  • 1
  • 1
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

You will have to use NSUserDefault or Singleton to handle the values of varaiables accross various Controllers.

These both are used in such a scenario where you need to maintain and access the variable values across multiple View Controllers. You can opt for either one based on your choice. NSUserDefault can store multiple key-value pairs that are accessible globally throughout the app. Singleton helps you create a object / variable which is static and hence no other instance of it is created afterwards. Only a single instance is retained throughout the app.

The following links might help you.

Singleton Tutorial

Another Singleton Guide

NSUserDefault Tutorial

Another NSUserDefault Tutorial

Hope this helps !

Community
  • 1
  • 1
Dhrumil
  • 3,221
  • 6
  • 21
  • 34
0

When you are returning from C to B basically you creating another another instance of B in memory which is not same as what you had when you came from A. Remove sgeue pushviewcontroller on backbutton just type following on backbutton IBAction click.

[self.navigationController popViewControllerAnimated:YES];

It will take you to B which you had when you came from A and and on viewWillAppear your value will remain there

Whenever you push your viewControllers it gets added to self.navigationalController ViewControllers array.

Shashi3456643
  • 2,021
  • 17
  • 21