Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

UI button freezes

UI button freezes when I click on it any help on what I could do to resolve this problem.

please post the code for your IBAction that is called when you press the button

  • (IBAction)btnMenuTapped:(id)sender { if (self.popoverController) { [self.popoverController dismissPopoverAnimated:YES]; self.popoverController = nil; } else { //below is where the code freezes MainMenuViewController *contentViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController" bundle:nil]; contentViewController.delegate = self;

    self.popoverController = [[WEPopoverController alloc] initWithContentViewController:contentViewController];
    
    self.popoverController.delegate = self;
    self.popoverController.popoverContentSize = contentViewController.view.frame.size;
    
    [self.popoverController presentPopoverFromRect:[sender frame]
                                            inView:self.view
                          permittedArrowDirections:UIPopoverArrowDirectionDown
                                          animated:YES];
    

    } }

What do you mean by "the button freezes"? Does the entire app freeze—sections that worked before no longer work? Or does only the button become unresponsive?

My guess is, you're trying to call contentViewController.view.frame.size before the contentViewController.view.frame.size has been added to the view hierarchy, and therefore it has no size. I might be wrong though...

Mike Baxter So how i might go about resolving this issue? I mean when I'm testing it, the entire app crashes.

Orbin, why did you subclass UIPopoverController into "WEPopoverController"? I wonder if that's causing the issue, but it's hard to tell as the code for it isn't listed. What happens if you just allocate a "UIPopoverController" instead? (You'll have to declare it as UIPopoverController in your header/interface file as well.)

So I'm suggesting, change the first line to

self.popoverController = [[UIPopoverController alloc] initWithContentViewController:contentViewController];

And make sure in your header it's

UIPopoverController *popoverController;

The best thing you can learn to do here (which will save you tons of time in the future) is to learn how to use breakpoints for debugging. I would set up a breakpoint and step through each (relevant) line until the app crashes. So start with that first line of code I just mentioned, and then move forward through the rest of the code you posted. You want to figure out which line causes the crash. Once you figure that out, post the code for that line—as well as the console crash report—here and we'll see if we can help you with that. I wish I could be more helpful and explain breakpoints, but I learned from an iOS programming book and Apple videos, so I'm not sure if/where a Team Treehouse resource is to help you. There's always Google searches, and Apple has some really good—but really thorough and verbose—documentation.

So yeah, try breakpoints and see if you can report back to us the results, or if they help you figure it out on your own. I'll caution you though that a breakpoint brings Xcode to the forefront, so sometimes it looks like your app has crashed when Xcode is merely showing you the source code and the option to continue on with the code. Apple's crash warnings are fairly helpful though, so that's the kind of thing you're looking for in the end—you either want to find the line of code that crashes it and figure out why it's doing that, or decipher the crash log.

Mike Baxter thanks a lot for your help, Im still learning myself and just like yourself Ive been self teaching myself. And I was working on this right before iOS 7 was released and i didn't get the crash. But after updating Xcode this problem and several other rose. But, ill try this method and ill let you know my findings. Also, I will post the console crash report once i find out whats going on.