Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
834 views
in Technique[技术] by (71.8m points)

ios - modal view controller not calling presenting view controller's dismissModalViewControllerAnimated: method

In my modal view controller I have a button handling method that includes

[self dismissModalViewControllerAnimated: YES];

In the presenting view controller I override dismissModalViewControllerAnimated: as follows:

-(void) dismissModalViewControllerAnimated: (BOOL)animated
{
  NSLog(@"dismiss");
  [super dismissModalViewControllerAnimated: animated];
}

When the button is touched, the button handling method gets called, but the dismissModalViewControllerAnimated: override does not seem to get called: the NSLog(@"dismiss"); statement isn't called, and a breakpoint inside the method doesn't get hit.

I tried

[[self presentingViewController] dismissModalViewControllerAnimated: YES];

but that didn't work either. However, the modal view controller does get dismissed.

Any idea what might be going wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is normally handled by declaring your presenting view controller as a delegate for your modal view controller. The modal VC then called a delegate method in the presenting VC to dismiss the modal transition it created.

Example:

Modal VC.h:

@protocol ModalViewControllerDelegate
-(void)dismissMyModalViewController;
@end

Modal VC.m:

// When you want to dismiss the Modal VC
[delegate dismissMyModalViewController]; 

Presenting VC.h:

// Make sure to #import ModalVC.h
@property (nonatomic, retain) id <ModalViewControllerDelegate> delegate;

Presenting VC.m:

-(void)dismissMyModalViewController {
    [self dismissModalViewControllerAnimated:YES];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...