开发者

dismissModalViewControllerAnimated not working

开发者 https://www.devze.com 2023-02-08 10:16 出处:网络
I have a UITabbarController with a UINavigationController. The view in the NavigationController presents an MVC (AddClockNavigationController). This MVC has a nib with an view and an UINavigationContr

I have a UITabbarController with a UINavigationController. The view in the NavigationController presents an MVC (AddClockNavigationController). This MVC has a nib with an view and an UINavigationController. The NavigationController.view is added as an subview to the MVC's view.

The NavigationController in my MVC has two buttons, an Cancel and an Done. When pressing either one of them the MVC should be dismissed. The IBActions of these buttons are in the UINavigationController's view (AddClockViewController), but whenever pressing them the MVC does not dissappear. If I put the 开发者_开发技巧actions into the MVC, it does disapear. If I put the function in my MVC and call it from my viewController using [self.parentViewController myFunction] it does not disappear.

How can I get it to disappear from my view?

I guess my explenation is quite messy but I hope you understand it.

A view from my IB:

dismissModalViewControllerAnimated not working

Best regards,

Paul Peelen

Edit I have tried this with [self dismissModalViewControllerAnimated:YES];, [self.parentViewController dismissModalViewControllerAnimated:YES];, [self.navigationController dismissModalViewControllerAnimated:YES];, [self.parentViewController.navigationController dismissModalViewControllerAnimated:YES]; and, as stated, with a function in the parentView dismissing it. Nothing works.


When some view controller presents another one, it becomes a parent view controller. So if you want to dismiss your modal view controller, then you have to choices:

  1. from the parent view controller call

    [self dismissModalViewControllerAnimated: YES];

OR

  1. from the modal view controller call

    [self.parentViewController dismissModalViewControllerAnimated: YES];

You actually don't need the navigation controller (cause you don't push/pop any view controllers). Just add the UINavigationBar to your MVC and place buttons on it. Then add actions to them and do choice #2. Your code is not working because navigation controller has no parent, as it wasn't presented as modal.


One situation is that dismiss{Modal}ViewControllerAnimated is called before presented viewController's viewDidAppear is called. In such situation, the presented is presented with animated == YES. and before the presented is fully appeared, it got dismissed.

To fix it, you need to check at the point of time for dismissing, check if viewDidAppear is called, if not, defer the dismissing in - viewDidAppear.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号