ios - Detect when a presented view controller is dismissed -


let's say, have instance of view controller class called vc2. in vc2, there "cancel" button dismiss itself. can't detect or receive callback when "cancel" button got trigger. vc2 black box.

a view controller (called vc1) present vc2 using presentviewcontroller:animated:completion: method.

what options vc1 have detect when vc2 dismissed?

edit: comment of @rory mckinnel , answer of @nicolasmiari, tried following:

in vc2:

-(void)cancelbutton:(id)sender {     [self dismissviewcontrolleranimated:yes completion:^{      }]; //    [super dismissviewcontrolleranimated:yes completion:^{ //         //    }]; } 

in vc1:

//-(void)dismissviewcontrolleranimated:(bool)flag completion:(void (^)(void))completion - (void)dismissviewcontrolleranimated:(bool)flag                            completion:(void (^ _nullable)(void))completion {     nslog(@"%s ", __pretty_function__);     [super dismissviewcontrolleranimated:flag completion:completion]; //    [self dismissviewcontrolleranimated:yes completion:^{ //         //    }]; } 

but dismissviewcontrolleranimated in vc1 not getting called.

according docs, presenting controller responsible actual dismiss. when presented controller dismisses itself, ask presenter it. if override dismissviewcontrolleranimated in vc1 controller believe called when hit cancel on vc2. detect dismiss , call super classes version actual dismiss.

as found discussion not seem work. rather rely on underlying mechanism, instead of calling dismissviewcontrolleranimated:completion on vc2 itself, call dismissviewcontrolleranimated:completion on self.presentingviewcontroller in vc2. call override directly.

a better approach altogether have vc2 provide block called when modal controller has completed.

so in vc2, provide block property name ondoneblock.

in vc1 present follows:

  • in vc1, create vc2

  • set done handler vc2 as: vc2.ondoneblock={[vc2 dismissviewcontrolleranimated:yes completion:nil]};

  • present vc2 controller normal using [self presentviewcontroller:vc2 animated:yes completion:nil];

  • in vc2, in cancel target action call self.ondoneblock();

the result vc2 tells whoever raises it done. can extend ondoneblock have arguments indicate if modal comleted, cancelled, succeeded etc....


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -