简体   繁体   中英

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. I used a protocol but does not work.

In ViewControllerB.h

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

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

In ViewControllerB.m

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

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

In ViewControllerA.h

@interface ViewControllerA: UIViewController <detailProgrammFiereDelegate>

In 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.

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. 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.

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. So A's -addItemViewController: will be called at returning time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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