0

I have a UILabel to which I add a UITapGestureRecognizer:

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nomLabelClicked)];
[nomLabel setUserInteractionEnabled:YES];
[nomLabel addGestureRecognizer:tap];
nomLabel.text = nom;
nomLabel.adjustsFontSizeToFitWidth = YES;
nomLabel.minimumScaleFactor = 0.6;

The method I call does this:

-(void)nomLabelClicked{
CGSize labelNomSize = [nomLabel.text sizeWithAttributes:@{NSFontAttributeName : nomLabel.font}];
if ((labelNomSize.width * 0.6) > nomLabelView.bounds.size.width) {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"AutoLayoutStoryboard" bundle:nil];
    ViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:@"ViewName"];
    UINavigationController *destNav = [[UINavigationController alloc] initWithRootViewController:sfvc];
    sfvc.preferredContentSize = CGSizeMake(self.view.frame.size.width * 0.8, self.view.frame.size.height * 0.1);
    destNav.modalPresentationStyle = UIModalPresentationPopover;
    typePopover = destNav.popoverPresentationController;
    typePopover.delegate = self;
    typePopover.sourceView = self.view;
    typePopover.sourceRect = labelView.frame;
    destNav.navigationBarHidden = YES;
    dispatch_async(dispatch_get_main_queue(), ^(void){
        [self presentViewController:destNav animated:YES completion:nil];
    });
}

When I run this and click on the label, I first get this:

*** Assertion failure in void _UIPerformResizeOfTextViewForTextContainer(NSLayoutManager *, UIView<NSTextContainerView> *, NSTextContainer *, NSUInteger)(), /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIFoundation_Sim/UIFoundation-432.1/UIFoundation/TextSystem/NSLayoutManager_Private.m:1551

Then I wait a couple of seconds and the app crashes, and I get this:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'

I've tried adding the dispatch_async(dispatch_get_main_queue(), ^(void){ as you can see but it hasn't changed anything. I understand the error: it seems as if I'm changing the UI somewhere in the background thread but I can't seem to understand exactly where and change it. The weird part is that I can't seem to pinpoint the issue with breakpoints. The view that is presented comes clean through all the breakpoints, and the method "nomLabelClicked" too. I have a breakpoint on "All Exceptions" but it just shows me the error, it doesn't break somewhere.

el-flor
  • 1,466
  • 3
  • 18
  • 39

0 Answers0