我有两个viewController,viewControllerB,当通过navigationBar的后退按钮返回viewControllerA时,我希望你传递一个 bool 值。我使用了一个协议(protocol),但不起作用。
在ViewControllerB.h中
@protocol detailProgrammFiereDelegate <NSObject>
@required
-(void) addItemViewController: (ViewControllerB *)programmFiere withBool:(BOOL)booleanFiere;
@end
.....
@property (nonatomic, weak)id <detailProgrammFiereDelegate>delegate;
.......
在ViewControllerB.m中
- (void)viewDidLoad
{
......
BOOL booleanFiere =YES;
[self.delegate addItemViewController:self withBool:booleanFiere];
}
在ViewControllerA.h中
@interface ViewControllerA: UIViewController <detailProgrammFiereDelegate>
在ViewControllerA.m中
-(void)addItemViewController:(DetailProgrammFiere *)programmFiere withBool:(BOOL)booleanFiere{
//after pressing back button of viewControllerB not enter into this method. Why?
if (booleanFiere){ //is already true before opening the ViewControllerB. Why?
[self viewDidLoad];
}
}
........
-(void)getInformationsFiere:(id)sender{ //method that open ViewControllerB
ViewControllerB * detailFiere =[[ViewControllerB alloc]initWithNibName:@"ViewControllerB~iPhone" bundle:nil];
detailFiere.delegate =self;
[self.navigationController pushViewController:detailFiere animated:YES];
}
在打开 ViewControllerB 之前 bool 值已经为 true,这不应该发生。
最佳答案
如果要从B返回A时传递参数,则不应将调用委托(delegate)方法放在viewDidLoad中。 B 的 viewDidLoad 会在 B 被 alloc 和 init 的时候调用,但在 pop B 返回到 A 的时候不会被调用。这也是为什么在 B 出现之前,A 的 booleanFiere 就已经是 YES 的原因了。
你可以放
[self.delegate addItemViewController:self withBool:booleanFiere];
就在 B 的 [self.navigationController popViewControllerAnimated:YES] 之前,而不是在 B 的 viewDidLoad 中。所以A的-addItemViewController:会在返回时被调用
关于ios - 通过导航栏的后退按钮传递 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13836809/