繁体   English   中英

通过NavigationBar的布尔槽后退按钮

[英]Pass boolean trough back button of navigationBar

我有两个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:将在返回时被调用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM