Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
836 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 Reply

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];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...