0

我自定义了后退按钮的动作。如果按下返回,我想向父视图发送一个 BOOL,但布尔值始终为空。

我的父母.h


    [...skip...]

    BOOL myBool;

    [...skip....]

我的父母.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];
}

在我的孩子.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];
}

当父级出现时,myBool 为空。

如果我改变


    [self.navigationController popViewControllerAnimated:YES];


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

一切正常,但由于几个原因不是我想要的。

任何帮助表示赞赏。

谢谢,马克斯

4

2 回答 2

2

您没有将 bool 传递回父级,而是创建了一个全新的对象并将 bool 提供给它!

看看这一行:

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

那行已经创建了一个新的父对象。您可能想使用原始父对象:)

在theChild.h

[snip]
theParentVC *parent;
[snip]

当你创建孩子

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

当你想更新父母

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

    // dismiss child view
    [self.navigationController popViewControllerAnimated:YES];
}
于 2011-02-28T13:38:26.377 回答
0

您正在创建一个新的视图控制器,而不是链接回真正的父级。

尝试

self.parentViewController.myBool = YES;

而不是

theParent *theParentVC = [[addSite alloc] init];
// set parent BOOL
theParentVC.myBool = YES;
于 2011-02-28T13:34:48.080 回答