按下后退按钮时传递BOOL
问题描述:
我自定义了后退按钮的动作。我想发送到父视图一个布尔如果后面被按下,但布尔值始终为空。按下后退按钮时传递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];
}
在我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];
}
当父出现,myBool为空。
如果我改变
[self.navigationController popViewControllerAnimated:YES];
到
[self.navigationController pushViewController:theParentVC animated:YES];
所有工作正常,但不是我想有几个原因。
任何帮助表示赞赏。
感谢, 最大
答
你没有通过布尔回父,你要创建一个完全新的对象,并给予了布尔而不是!
看这句话:
theParent *theParentVC = [[addSite alloc] init];
该行取得了新的父对象。你可能想,当你创建子
-(IBAction)callTheChild:(id)sender {
theChild *theChildVC = [[theChild alloc] initWithNibName:@"theChild" bundle:nil];
[theChild setParent:self];
[self.navigationController pushViewController:theChildVC animated:YES];
[theChildVC release];
}
使用原来的父对象:)
在theChild.h
[snip]
theParentVC *parent;
[snip]
,当你想更新父
-(void)backAction:(id)sender {
// Update the parent
parent.myBool = YES;
// dismiss child view
[self.navigationController popViewControllerAnimated:YES];
}
答
您正在创建一个新的视图控制器,而不是链接回真正的父母。
尝试
self.parentViewController.myBool = YES;
而不是
theParent *theParentVC = [[addSite alloc] init];
// set parent BOOL
theParentVC.myBool = YES;
刚刚意识到我错过了'@property(nonatomic,retain)theParentVC * parent;'从Child.h中的行。另外不要忘记''父母释放];'在theChild的dealloc方法:)' – deanWombourne 2011-02-28 13:52:29
感谢您指出我在正确的方向。有效。 – masgar 2011-02-28 15:03:33