我有两个viewController,即viewControllerB,当通过navigationBar的后退按钮返回到viewControllerA时,我希望您传递一个布尔值。我使用了一个协议,但是不起作用。
在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之前,布尔值已经为true,这应该不会发生。
最佳答案
如果要在从B返回A时传递参数,则不应将调用的委托方法放在viewDidLoad中。当B是alloc和init时,将调用B的viewDidLoad,但在弹出B返回到A时不会调用B。这也是在B出现之前,A的booleanFiere已经为YES的原因。
你可以放
[self.delegate addItemViewController:self withBool:booleanFiere];
就在您B的[self.navigationController popViewControllerAnimated:YES]之前,而不是在B的viewDidLoad中。所以A的-addItemViewController:将在返回时被调用