3

In my App I'm using a Navigation controller. My problem is, when I go to previous view (using the generated navigation back button) my NSString data doesn't transfer over. I don't know how to save these NSString values when I navigate through the stack from navigation controller.I need to send an email with these NSString values.

This is my view controller before pushing back button

-(void)button{
    NSString *dd = @"text";
    viewcontroller.string = dd;
}

This is the previous controller after pushing the back button (view controller)

.h

@property (strong,nonatomic) NSString *string;

.m

NSString *emailBody = [NSString stringWithFormat:@"%@  ", string];

Also, I found this question that was SIMILAR to my situation and I model my question around it. UITextField contents are empty after popViewControllerAnimated If someone can look at it and see how you can apply this to my situation I will appreciate it.

Community
  • 1
  • 1

2 Answers2

1

You should use delegation: the earlier (overview) controller (the one you navigate back to) should implement a protocol the detail view controller defines. the earlier controller will set itself as delegate of the detail controller just before presenting. when being presented and navigation back from detail to overview controller is about to happen, the detail controller can inform (aka send a message including the string as an parameter object)the overview controller just before it is dismissed.

vikingosegundo
  • 51,126
  • 14
  • 131
  • 172
  • Sorry, I am a bit of a noob to delegation.May you provide sample using my code (or something similar)? I will deeply appreciate it, you do not have to do it however if it takes too much of your time. I will research it ,but it may take me a while to figure it out. – Feliz Joneser Sep 01 '13 at 01:53
  • it is 5 o'clock in the morning in my place. so I will have a look if I find useful codes for u tomorrow. but so for your codes indicates, that your detail controller knows the previous controller. this seems to be a mutual dependency, you should with all means try to avoid it. with delegation only the overview controller will know the detail controller. while the detail controller only knows the contract for communication — the protocol it's delegate fulfills. – vikingosegundo Sep 01 '13 at 03:04
0

I agree with vikingosegundo, Delegation is the best way to handle this. I would suggest you to read this post: click here

But there is way out,

in your child view controller:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"back"                                                                 style:UIBarButtonItemStyleBordered
                                                                  target:self
                                                                  action:@selector(goBack:)];

    self.navigationItem.leftBarButtonItem = backButton;
}

- (void) goBack:(id)sender
{
    NSArray *vcs = self.navigationController.viewControllers;
    id parentVC = [vcs objectAtIndex:[vcs count] - 2];
    if ([parentVC respondsToSelector:@selector(getStringFromChildVC:)])
        [parentVC getStringFromChildVC:@"text"];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

and in your parent view controller .h file:

-(void)getStringFromChildVC:(NSString *)myString;

and .m file

-(void)getStringFromChildVC:(NSString *)myString
{
    self.string = myString;
}

That will be it.. But still suggest you to learn the delegation.

Community
  • 1
  • 1
Xu Yin
  • 3,932
  • 1
  • 24
  • 46