views:

26

answers:

2

I have an intermittent bug that is confounding me. Any advice on how to track it down or what might be the cause are greatly appreciated.

I have a "DetailView" with a few labels, an Image View and a Text View. In the navbar I also have a camera button to open an Image Picker and take a picture (later added to the image view). Basic stuff.

Sometimes, when taking a picture and then editing the text: the whole view between the navbar and the keyboard goes blank (to my background color). Happens more often the "first time". Repeating the procedure does not give the same problem. Happens almost only on the 3Gs (very rare on the 3G and the original iPhone).

I have two theories.

1 is that it has something to do with the scroll view that is the container for the disappearing GUI elements. The view hierarchy is: ScrollView -> UIView -> labels, texts and image. Is it a bad thing to have the scrollview as the "main" view?

2 is that it has to do with memory. The 3Gs has a better camera and takes bigger pictures... Possibly something happens if the app gets a low memory warning while taking the picture (not uncommon)?

Are any of these two at all feasible? Any other ideas on what to look for?

thanks

Update: Could two simultaneous animations cause the bug?

On the KeyboardWillShow notification I resize the ScrollView using the UIView beginAnimations ... commitAnimations and right after that (which happens asynchronously I believe) I also tell the scroll view to scrollRectToVisible for the TextView.

like this:

    [UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];

CGRect rect = [[self view] frame];
rect.size.height -= keyboardFrame.size.height * (up? 1 : -1); 
[[self view] setFrame: rect];

[UIView commitAnimations];

// Scroll the active text field into view.
DetailView *tempScrollView = (DetailView *) [self view];
CGRect textFieldRect = [comments frame];
[tempScrollView scrollRectToVisible:textFieldRect animated:YES];    
A: 

The 3GS also has twice the RAM of the 3G/original (256MB vs 128), so I doubt it's memory-related. You can override didReceiveMemoryWarning: to check, though.

When exactly does the view go blank? Does it happen spontaneously, or only when you press a certain button, etc. For example, does it only happen when you start editing the text view (i.e. when the keyboard pops up)? If so, check your view autoresizing masks.

Are the views actually gone, or are they just offscreen? Try printing their frames to the console or using the debugger to check where exactly they should be.

When you find the bug, please post your solution- it sounds like a problem I might encounter some day.

igul222
Good point about the RAM on the 3Gs.The view goes blank when I tap inside the text view. What should happen is that the keyboard pops up, the scroll view is resized and scrolled to show the TextView (which would otherwise be behind the keyboard). I have only seen the bug with my own eyes about three times total so I can't say for sure if things disappear at the start of the animation or the end. Possibly something can get messed up with the scrollview dimensions and everything is just pushed off screen?I'll see if I can borrow a 3Gs to test on. It is the one model I don't have :(
Martin Westin
A: 

I managed to track it down in the Sim. Here it goes:

  • Going to the DetailView
  • Tapping the camera buttom to "open" the UIImagePickerController (camera or Library, does not matter)
  • Now I simulate a memory warning before closing the Picker
  • Choose the image to close the Picker
  • Tap the UITextView

Under these circumstances the view will receive TWO UIKeyboardWillShowNotification are eachother... so my copied example code for resizing the view is run twise... making it 416px - 216px - 216px = -16px in height... not a good thing.

The reason for the double notifications were of-course that the ViewController added itself as an observer in viewDidLoad... which runs again when the view "appears" after the memory warning... but the ViewController never removed itself as an observer.

Doing that fixed the bug for sure:

- (void)viewDidLoad {
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];   

}

  • (void)viewDidUnload { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
Martin Westin