通过导航栏的后退按钮传递布尔值

时间:2012-12-12 09:39:08

标签: ios delegates protocols parameter-passing

我有两个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之前,boolean已经为true,这不应该发生。

1 个答案:

答案 0 :(得分:1)

如果要在从B返回A时传递参数,则不应将调用委托方法放在viewDidLoad中。当B是alloc和init时,将调用B的viewDidLoad,但是当弹出B返回A时不会被调用。这也是B出现之前的原因,A的booleanFiere已经是YES。

你可以把

    [self.delegate addItemViewController:self withBool:booleanFiere];

就在B的[self.navigationController popViewControllerAnimated:YES]之前,而不是在B的viewDidLoad中。所以A的-addItemViewController:将在返回时调用