0

I customized the action of back button. I want to send to parent view a BOOL if back is pressed, but the bool value is always null.

my parent .h


    [...skip...]

    BOOL myBool;

    [...skip....]

my parent .m


#import "theChild.h"

....


- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"myBool is %d", (int)myBool);
}

-(IBAction)callTheChild:(id)sender {
    theChild *theChildVC = [[theChild alloc] initWithNibName:@"theChild" bundle:nil];
        // set something
    [self.navigationController pushViewController:theChildVC animated:YES];
    [theChildVC release];
}

in my theChild .m



#import "theParent.h"
....
....
-(void)backAction:(id)sender {

    theParent *theParentVC = [[addSite alloc] init];
    // set parent BOOL
    theParentVC.myBool = YES;
    [addVC release];
    // dismiss child view
    [self.navigationController popViewControllerAnimated:YES];
}

when the parent appear, myBool is null.

if I change


    [self.navigationController popViewControllerAnimated:YES];

to


    [self.navigationController pushViewController:theParentVC animated:YES];

all works fine but is not what I want for several reasons.

Any help is appreciated.

Thanks, Max

masgar
  • 1,875
  • 2
  • 20
  • 32

2 Answers2

2

You're not passing the bool back to the parent, you're creating a completely new object and giving that the bool instead!

Look at this line :

theParent *theParentVC = [[addSite alloc] init];

That line has made a new parent object. You probably wanted to use the original parent object :)

in theChild.h

[snip]
theParentVC *parent;
[snip]

when you create the child

-(IBAction)callTheChild:(id)sender {
    theChild *theChildVC = [[theChild alloc] initWithNibName:@"theChild" bundle:nil];
    [theChild setParent:self];
    [self.navigationController pushViewController:theChildVC animated:YES];
    [theChildVC release];
}

and when you want to update the parent

-(void)backAction:(id)sender {
    // Update the parent
    parent.myBool = YES;

    // dismiss child view
    [self.navigationController popViewControllerAnimated:YES];
}
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • Just realised I've missed out the `@property (nonatomic, retain) theParentVC *parent;` line from theChild.h. Also don't forget to `[parent release];` in the dealloc method of theChild :) – deanWombourne Feb 28 '11 at 13:52
0

You are creating a new viewcontroller, not linking back to the true parent.

try

self.parentViewController.myBool = YES;

rather than

theParent *theParentVC = [[addSite alloc] init];
// set parent BOOL
theParentVC.myBool = YES;
Bongeh
  • 2,300
  • 2
  • 18
  • 30