Error when dismissing view controller

前端 未结 6 1702
时光取名叫无心
时光取名叫无心 2021-02-07 07:52

Getting an error when dismissing a view controller, not seen anything like it before, and not much about it on the internet.

heres the error: * Assertion failur

相关标签:
6条回答
  • 2021-02-07 07:56

    Make sure to dismiss the view controller from the presenting view.

    https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

    When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it.

    0 讨论(0)
  • 2021-02-07 08:03

    I recieved this error when I was calling -presentViewController:animated:completion: on a thread that was not the main thread (from a callback in a network request). The solution is to dispatch your calls to present and dismiss view controllers to the main thread:

    dispatch_async(dispatch_get_main_queue(), ^{
        //Code that presents or dismisses a view controller here
    });
    
    0 讨论(0)
  • 2021-02-07 08:10

    If someone is having Assertion Failure issue then here is the solution for Swift 3:

    OperationQueue.main.addOperation{
        <your segue or function call>
    }
    

    Tested On: Xcode 8.3.2 and Swift 3.1

    0 讨论(0)
  • 2021-02-07 08:11
    In the target view controller define delegate and protocol:
    
    @class TargetVC;
    @protocol TargetVCDelegate <NSObject>
    -(void)dismissTargetVC:(TargetVC*)vc;
    @end
    
    @interface TargetVC : UIViewController
    @property (nonatomic, weak) id<TargetVCDelegate> delegate;
    @end
    
    when you done the job at the target view controller call the delegate:
    if( [self.delegate respondsToSelector:@selector(dismissTargetVC:)])
    {
       [self.delegate dismissTargetVC:self];
    }
    
    The implementation of the delegate protocol should be:
    -(void)dismissTargetVC:(TargetVC*)vc
    {
        [vc dismissViewControllerAnimated:YES completion:nil];
    
        // you can get relevant data from vc as you still hold reference to it in this block
    
        // your code ...
    }
    
    0 讨论(0)
  • 2021-02-07 08:13

    You must make sure not only that present/dissmissViewController called on main thread but also you have to make sure that present/dismissViewController is called from the same parent viewController.

    For example there are two navigationController children. First child view controller presents the second child for some job that will return thru a delegate (interface). After the job is done second child dismisses itself and calls to delegate(interface) function with have to present another viweController (for example customPopup) -> that's rises the error since the second child view controller is not dismissed when the present of popup called, but already demised when the dismiss of popup called.

    So in this case:

      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(400 * NSEC_PER_MSEC)), dispatch_get_main_queue(), { () -> Void in
                if let fs = self.scenarios[indexPath.item]{
                    fs.loadScenario()
                    sDelegate.onSelectedScenario(fs)
                }
            })
    

    will do.

    0 讨论(0)
  • 2021-02-07 08:16

    I had the same problem while calling the camera view

    Swift syntax for the same problem:

    dispatch_async(dispatch_get_main_queue(), { 
        //Code that presents or dismisses a view controller here  
        self.presentViewController(imagePicker, animated: true, completion: nil)
    })
    
    0 讨论(0)
提交回复
热议问题