-->

iOS 7 error Warning: Attempt to dismiss from view

2020-07-18 03:39发布

问题:

I have issue in iOS 7 that does not appear in iOS 6.

I have a navigation controller that is displaying another navigation controller to add an employee. This second controller is being presented modally. When I dismiss the second controller either with a "Cancel" or "Done" button, I get an error. Here is the error:

QuickSchedule[880:60b] Warning: Attempt to dismiss from view controller while a presentation or dismiss is in progress!

I am using an unwind segue and dismissing from the first controller using the following relevant code.

This is in ScheduleViewController.m (My main controller window)

- (IBAction)done:(UIStoryboardSegue *)segue
{
    if ([[segue identifier] isEqualToString:@"DoneEditing"]) {
        [[MyManager sharedManager] saveChanges];
        [self dismissViewControllerAnimated:YES completion:NULL];
    }
}

The connection in the connection inspector for the "Done" button is simply "action -> [unwind done:]"

I had no errors before upgrading to Xcode 5. This all started after upgrading Xcode and my storyboard to iOS 7.

I am getting same error in different spot in my app, but again, it's with a modally presented view controller.

I go from EmployeeViewController to AddEmployeeViewController modally. I get the error again when I return from AddEmployeeViewController.

EmployeeViewController.m

- (IBAction)done:(UIStoryboardSegue *)segue
{
    if ([[segue identifier] isEqualToString:@"ReturnInput"]) {
        AddEmployeeViewController *addController = [segue sourceViewController];
        if (addController.employee) {
            [[MyManager sharedManager] saveChanges];
            [[self tableView] reloadData];
        }
        if (![self.presentedViewController isBeingDismissed]) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
}

- (IBAction)cancel:(UIStoryboardSegue *)segue
{
    if ([[segue identifier] isEqualToString:@"CancelInput"]) {
        [self dismissViewControllerAnimated:YES completion:NULL];
    }
}

Here is AddEmployeeViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"ReturnInput"]) {
        if ([self.firstNameField.text length] || [self.lastNameField.text length]) {

            Employee *newEmployee = [[MyManager sharedManager] createEmployeeWithFirstName:self.firstNameField.text andLastName:self.lastNameField.text];
            [[MyManager sharedManager] addEmployeeToList:newEmployee];
            self.employee = newEmployee;
        }
    }
}

I am still learning, and I have looked for hours online and cannot find an answer to this. I have tried putting the "saving code" in a completion block. I put it back and tried using nil instead of NULL in the completion block argument. As you can see, I have nil in one spot on a completion block argument and NULL on another. No matter what, the error keeps appearing.

Everything works as far a functionality, I just get this error logged to the console. Any help is greatly appreciated.

Note: I do not get this error with regular pushed navigation controllers. This is only happening on dismissing modally presented view controllers.

回答1:

I'm surprised that you wouldn't see this same problem in previous versions of Xcode, because I think your problem is calling dismissViewControllerAnimated:completion: in the "done" method. This should have been a problem in iOS 6 as well. The unwind segue does the dismissal for you, so you shouldn't call this method yourself. Try commenting it out, and see if that fixes the problem.



回答2:

I had this very same problem in only iOS 7, too.

My problem was to call methods that works on the UI (like showing UIAlertView etc.) on the viewWillAppear: method. That was quite wrong because when one view was being dismissed, another view was appearing so both UI methods were being called.

I have moved my methods to viewDidAppear: method and the problem have been fixed.



回答3:

Recheck your code. You have an animation that hasn't completely finished prior to firing another animation transition. I had the same issues and tracked it down to multiple animations slightly overlapping. Set dismissViewControllerAnimated:NO and you'll see what I mean.



回答4:

here is the real answer to this question..! most of the time you programmer don't pay attention to events enum in the apple iOS sdk... if you use events like this [ UIControlEventAllEvent ] on a [UIButton] and run a [dismissViewControllerAnimated] note that the [ UIControlEventAllEvent ] contains lots of other events and you are dismissing the view before it finishes all other events inside the enum type of [ UIControlEventAllEvent ] !! so the guys if you just want a click on the button just use [UIControlEventTouchUpInside] and it will give you this error!!!



回答5:

Warnings of this type specific to iOS7 and not iOS6 do occur in other situations. In my case it was in relation to the dismissal of an email alert. I only had to add the conditional structure in the method below with iOS7.

- (void)didPresentAlertView:(UIAlertView *)alertView
{
    if(![[self modalViewController] isBeingDismissed])
    {
        [self dismissModalViewControllerAnimated:YES];
    }
}