53
votes

I have a tab bar based app. There are navigation controllers in all 5 tabs with instances of custom view controller setup properly as root view controllers. This loads just fine. A couple of these view controllers contain table views. I want to show a modal view controller to the user when they select a row in the table view. The (relevant part of) didSelectRowAtIndexPath delegate method looks as follows:

SampleSelectorViewController *sampleVC = [[SampleSelectorViewController alloc] init];
[self presentViewController:sampleVC animated:YES completion:NULL];

The modal view controller appears BUT it appears after a very noticeable delay. At times it even requires the user to tap the row a second time. A few things that I have already verified are:

  • Table view's didSelectRowAtIndexPath method is called when the user taps the row
  • The didSelectRowAtIndexPath method does not contain any blocking calls. There are no network operations being performed and the modal view controller's setup does not involve any processing intensive task. The data it displays is static.
  • If I push the new view controller onto the navigation stack (everything else remaining exactly same), it behaves perfectly without any delays. It is only when presented modally that the delay is encountered.

Any ideas/suggestions?

10
Out of interest, is it equally slow with animated:NO?pbasdf
It is. The animation seems to have no effect on this odd behaviour.Numan Tariq
interesting. i have the same problem of a modal presentation being delayed (or having to tap the screen to make it appear). in my case, its not directly, but indirectly triggered by didSelectRowAtIndexPath. that calls a delegate methods, which calls a delegate method, which presents modally. hmm..Alex Bollbach
sounds very similar to the scenario I had. Would you mind sharing how you solved the problem? I have never seen the issue before or since and the scenario for this no longer exists in the project so can't be much help myselfNuman Tariq
Same issue here on iOS 11, Xcode 9, no luck yet..Stas

10 Answers

76
votes

It seems calling presentViewController:animated:completion from within tableView:didSelectRowAtIndexPath: is problematic. It's difficult to find anything that stands out when using the Time Profiler in Instruments, also. Sometimes my modal view comes up in less than a second and other times it takes 4s or even 9s.

I think it's related to the underlying UIPresentationController and layout, which is one of the few things I see when selecting the region of time between tapping on a row and seeing the modal presentation in the Time Profiler.

A Radar exists describing this issue: http://openradar.appspot.com/19563577

The workaround is simple but unsatisfying: delay the presentation slightly to avoid whatever contentious behavior is causing the slowdown.

dispatch_async(dispatch_get_main_queue(), ^{
   [self presentViewController:nav animated:YES completion:nil];
});
11
votes

If you call present(:animated:completion:) in tableView(:didSelectRowAt:), selectionStyle == .none for selected tableview cell and you’ve got this strange behavior then try to call tableView.deselectRow(at:animated:) before any operations in tableView(_:didSelectRowAt:).

Did it help?

7
votes

Swift 4: you can use as below.

DispatchQueue.main.async {
            let popUpVc = Utilities.viewController(name: "TwoBtnPopUpViewController", onStoryboard: "Login") as? TwoBtnPopUpViewController
            self.present(popUpVc!, animated: true, completion: nil)
        }

It works for me.

5
votes

I guess you also set the cell's selectionStyle to UITableViewCellSelectionStyleNone. I change to UITableViewCellSelectionStyleDefault and it work perfect.

3
votes

You should display it modally from your root vc (e.g: customTabBarRootViewController). save a reference, and use the reference controller to display it.

3
votes

I have also had this strange delay when presenting from tableView:didSelectRowAtIndexPath: looks like an Apple bug.

This solution seems to work well though.

CFRunLoopWakeUp(CFRunLoopGetCurrent()); // Fixes a bug where the main thread may be asleep, especially when using UITableViewCellSelectionStyleNone
1
votes

Solution in Swift 3

In the SampleSelectorViewController(the presented view controller) use the below code

DispatchQueue.global(qos: .background).async {

// Write your code

}
1
votes

The common problem with this behaviour is as follows:

one sets selectionStyle = .none for a cell in the tableView (it seems that it doesn't depend on UITableViewController subclassing as was written at http://openradar.appspot.com/19563577) and uses at the delegate method

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

animating deselect

tableView.deselectRow(at: indexPath, animated: true)

which implies animation for non-animating cell.

In this case the subsequent view controller presentation has a delay.

There are some workaround solutions including dispatch_async on main thread, but it is better not to call deselectRow even without animation on unselectable cells in your code.

1
votes

As per @Y.Bonafons comment, In Swift, You can try like this, (for Swift 4.x & 5.0)

DispatchQueue.main.async {

                self.showAction() //Show what you need to present
            }
0
votes

Try this For swift version 5.2 you can use below code:

DispatchQueue.main.async(execute:{self.present(nav, animated: true)})