4

I am presenting a view controller modally in iOS app. The issue is that there is no crash and the app freezes as soon as presentViewController:animated is called. The stats show the CPU usage to be 100% and the usage doesn't go down even after manually closing the app.

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
CustomModalViewController *vvc = [sb instantiateViewControllerWithIdentifier:@"CustomModalViewController"];
if(!vvc){
    NSLog(@"ERROR!!! vvc is null");
}
NSLog(@"instantiate modal view controller");

vvc.providesPresentationContextTransitionStyle = YES;
vvc.definesPresentationContext = YES;
vvc.data = data;
NSLog(@"before presenting modal view controller");
[vvc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self presentViewController:vvc animated:YES completion:nil];

I tried printing some debug statements in the viewDidLoad of my custom class, but those are also not getting called.

I don't understand why the view controller is not being displayed. Any help will be appreciated. I want to know in what case does your app go into infinite loop on pushing a view controller or is it because of some other cause??

UPDATE:

This error occured after I updated to XCode 7. Not sure, but I guess this might be an issue with new SDK- the UIKit or LLVM compiler. I copied my project to another mac with Xcode 6.4 and the error disappears!!! I haven't changed any build settings either that would cause the issue.

Any pointers on how to proceed?

Ajax
  • 1,689
  • 4
  • 20
  • 29
  • do u want transparent background after presenting the other view controller? – Mukesh Sep 23 '15 at 10:31
  • yes. I want to show background view also. – Ajax Sep 23 '15 at 10:33
  • it will not work ma den.u may have to write custom transition to make background semi-transparent – Mukesh Sep 23 '15 at 10:34
  • I also tried UIModalPresentationPopover. getting the same issue. Any idea why this might happen?? I there was a crash I could have examine the stack trace at least. – Ajax Sep 23 '15 at 10:35
  • if u want i have shared on example here to make transparent background.http://stackoverflow.com/a/32434166/3535583 – Mukesh Sep 23 '15 at 10:38
  • Where are you calling this code from in the presenting controller? It is most likely an issue with the controller being presented. Try accessing the new controllers view before presenting it and see if it hangs. `let view=vvc.view` will force `viewDidLoad` to be called on vvc. If it now hangs on this line, your issue is in the CustomModalViewController. Probably in its `viewDidLoad`. You can also pause the debugger and see where it is in the stack trace. – Rory McKinnel Sep 23 '15 at 11:07
  • It hangs on that line so the issue seems to be in customModalViewController. But I have added a print statement on the first line in viewDidLoad and its not getting executed. I'll update by running timeProfiler and getting an idea of what method is stuck in the infinite loop. – Ajax Sep 23 '15 at 11:38
  • Its recursively calling `initWithCoder` in UIViewController loadViewIfRequired. – Ajax Sep 23 '15 at 11:45
  • Worth checking how you have defined the `CustomModalViewController`. Make sure you have only set this class for the view controller and not its main view as well etc... It sounds like you create it and in its creation it is creating another and so on. – Rory McKinnel Sep 23 '15 at 13:42
  • Saw your update. Have you tried deleting the app, doing a build clean, full build and then running in case something is not right with the app install. – Rory McKinnel Sep 24 '15 at 14:23
  • Ya, I tried clean and build. Deleted the app from device and then installed. I have switched to Xcode 6.4 for now. I'll try to replicate the bug in another project and post the github link so that others can try it out. – Ajax Sep 24 '15 at 14:32
  • @Ajax .. Yes also have the same problem earlier in Xcode 6.4 it was working perfectly fine but as Xcode gets updated then its freezes UI and CPU usage are 100% see [MY POST](http://stackoverflow.com/questions/32765545/uiviewcontroller-hangs-in-xcode-7) here – Rahul Shirphule Sep 28 '15 at 09:58

4 Answers4

6

Ok, this is bizarre, but I hope it helps: I have the EXACT same issue, the CPU jumps to 100% and view never shows. Works perfectly well in Xcode 6.4. In Xcode 7.1, on the view that I am calling, I have got a UITextView with some placeholder text "Notes:". What I found is that if I clear out the placeholder text, the view LOADS and all of the procedures execute as normal. If the placeholder text length is greater than 9 characters, the view also loads and procedures execute as normal. If the placeholder text length >0 and < 10, it's a no go. No view and CPU at 100%. This is odd, I realize, but hopefully it helps you out. Like you, I've got no errors or console output to show, it just spins.

EDIT FYI, blanking out the placeholder text in the storyboard, and just setting it in code fixes this as well irrespective of length

CodeNoob
  • 250
  • 3
  • 9
  • Holy smokes, that's it! I've been trying so many things and this was it. Same deal, Xcode 7.1, UITextView where I had placeholder copy on the storyboard and was setting it in my view controller. The solution for me was just to empty out the text on the storyboard and set it only in code. That explains why it's getting stuck in a NSString isEqualToString loop, sort of :) – Will Oct 14 '15 at 06:58
  • This doesn't give the root cause, but I am accepting it anyway!!! Worked in my case too.. – Ajax Oct 15 '15 at 05:43
  • please find an appropriate name for this question.it redirect people to this question and solution for another question – Muhammad Asyraf Mar 09 '20 at 07:34
  • Ι have a project that used to be in Swift 3 and Xcode 8.3. Now I had to switch to Xcode 11 and a few controllers don't appear when presented. The CPU and Memory go up and the screen freezes with no errors. But I don't see any UITextViews. Any other reasons ? – PoolHallJunkie May 01 '20 at 22:05
0

Same issue here.

I have a UITextView with the default string " Text " and fails with 100% CPU load. I was putting the placeholder only in code but until I remove the " Text " from the storyboard didn't work.

My code was developed in Xcode 6 and I noticed this error after send a new app revision builded with xcode 7.

Hexmit
  • 11
0

I had the same exact problem as described in the OP and the answers:

  1. Present a view controller (in my case a PageViewController)
  2. The presented view controller has an button that when pressed presents another view controller to add a note
  3. Move the app to the background while the "note" view controller is being presented. Then bring it back to the foreground.
  4. The app is now frozen. When connected to the debugger, it seems to be frozen indefinitely, but when running in prod it crashes. The CPU was pegged at 100% and the memory usage was climbing.

I ultimately tracked this down to my "Note" view being set as the First Responder, When my view appears, via override func viewWillAppear(_ animated: Bool), I set the note UITextView to become the first responder, noteTextView.becomeFirstResponder().

If I removed that line and never tapped the note field, my app would successfully resume from the background.

To fix this problem, I ended up adding observers for when the app moves to the background and moves into the foreground. In these observers, I make the note text view become and resign firstResponder, respectively.

    override func viewDidLoad() {
        super.viewDidLoad()
        //set up your view controller here. 

        NotificationCenter.default.addObserver(self, selector: #selector(self.appMovedToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(self.appMovedToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
    }

    @objc func appMovedToForeground() {
        self.noteTextView.becomeFirstResponder()
    }

    @objc func appMovedToBackground() {
        self.noteTextView.resignFirstResponder()
    }

I'm not sure if I should be doing something differently to prevent this problem, or if it's best practice to resign first responder when the app goes into the background, but this seems to work for me.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
0

I was presenting settings screen, so

I converted this:

    private lazy var settingsVC: SettingsVC = {
    let controller = SettingsVC.fromStoryboard()
    settingsVC.delegate = self
    return controller
}()

To this:

private lazy var settingsVC = SettingsVC.fromStoryboard()

And set the delegate later in view did load on controller which was presenting the settings:

    override func viewDidLoad() {
    super.viewDidLoad()
    settingsVC.delegate = self
}

And now it works