简体   繁体   中英

Modal window not being dismissed

I have two programmatically created buttons you can see in my viewDidLoad method. On the modal window I have a button that calls the cancelSearch method via a delegate. When I place a breakpoint on my cancelSearch method it is hit, so I know my delegate is set up correct, but even though it calls this line [self dismissViewControllerAnimated:YES completion:nil]; it's not actually closing the modal window.

The code below is all from my main controller view.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self  action:@selector(showActionMenu:)];
    actionButton.style = UIBarButtonItemStyleBordered;

    UIBarButtonItem *searchButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self  action:@selector(showSearchMenu:)];
    searchButtonItem.style = UIBarButtonItemStyleBordered;

    UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 103.0f, 44.01f)];
    NSArray* buttons = [NSArray arrayWithObjects:actionButton, searchButtonItem, nil];
    [toolbar setItems:buttons animated:NO];
    self.navigationItem.title = @"Census Management";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];


    [[RKClient sharedClient] get:@"censusmanagement" delegate:self]; 
}

- (IBAction)showActionMenu:(id)sender
{
    [self performSegueWithIdentifier: @"CMActionSegue" sender: self];
}

- (IBAction)showSearchMenu:(id)sender
{
    ehrxCMSearchView *search = [[self storyboard] instantiateViewControllerWithIdentifier:@"cmSearch"];
    search.selectedOptions = self.selectedOptions;

    search.delegate = self;

    [self.navigationController pushViewController:search animated:YES];
}

- (void)cancelSearch:(ehrxCMSearchView *)controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

You would dismiss a modal view using something similar to:

[self dismissModalViewControllerAnimated:YES];

This will dismiss the modal view which was loaded using something similar to:

[self presentModalViewController:search animated:YES];

However looking at your code snippet, it appears the search view controller is being pushed onto the navigation stack using the following line:

[self.navigationController pushViewController:search animated:YES];

So I you probably need to pop the view from the navigation stack rather than trying to dismiss it as a modal view:

[self.navigationController popViewControllerAnimated:YES];

If your view controller is modally presented, you should use this:

[self.presentingViewController dismissModalViewControllerAnimated:YES];

The presentingViewController property is available in iOS 5 only. So, if you're targeting older versions of iOS, you have to use self.parentViewController instead (use the appropriate one for each iOS version, you have to handle this).

If you make this control in your parent/presenting view controller, then just call this:

[self dismissModalViewControllerAnimated:YES];

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