简体   繁体   English

通过NavigationBar的布尔槽后退按钮

[英]Pass boolean trough back button of navigationBar

I have two viewController, the viewControllerB, when returning to viewControllerA through the back button of navigationBar, I want you pass a Boolean value. 我有两个viewController,即viewControllerB,当通过navigationBar的后退按钮返回到viewControllerA时,我希望您传递一个布尔值。 I used a protocol but does not work. 我使用了一种协议,但不起作用。

In ViewControllerB.h 在ViewControllerB.h中

 @protocol detailProgrammFiereDelegate <NSObject>
 @required
 -(void) addItemViewController: (ViewControllerB *)programmFiere withBool:(BOOL)booleanFiere;
 @end

 .....
 @property (nonatomic, weak)id <detailProgrammFiereDelegate>delegate;
 .......

In ViewControllerB.m 在ViewControllerB.m中

- (void)viewDidLoad
{
     ......

     BOOL booleanFiere =YES;
    [self.delegate addItemViewController:self withBool:booleanFiere];
 }

In ViewControllerA.h 在ViewControllerA.h中

@interface ViewControllerA: UIViewController <detailProgrammFiereDelegate>

In ViewControllerA.m 在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];

   }

The boolean is already true before the opening ViewControllerB and this should not happen. 在打开ViewControllerB之前,布尔值已经为true,这应该不会发生。

If you want pass a parameter when you want to return to A from B, you should not put the calling delegate method in the viewDidLoad. 如果要在从B返回A时传递参数,则不应将调用的委托方法放在viewDidLoad中。 The B's viewDidLoad will be called when B is alloc and init, but not when return to A by pop B. That is also the reason why before B shows up, A's booleanFiere is already YES. 当B是alloc和init时,将调用B的viewDidLoad,但在弹出B返回到A时将不调用B。这也是在B出现之前,A的booleanFiere已经为YES的原因。

You can put 你可以放

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

just before your B's [self.navigationController popViewControllerAnimated:YES] instead of in the B's viewDidLoad. 就在您B的[self.navigationController popViewControllerAnimated:YES]之前,而不是在B的viewDidLoad中。 So A's -addItemViewController: will be called at returning time 所以A的-addItemViewController:将在返回时被调用

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

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