I am doing a scientific visualization app for iPhone. Periodically a UIView subclass will receive some data from the network that it must render via its overridden drawRect: method.
My challenge is that there is no way to indicate to Cocoa-Touch that this data has arrived - or changed - other then [myView setNeedsDisplay] which is ignored except for changes to the bounds of the UIView. I have tried hiding and unhiding. Nuthin'.
Maddeningly, since all I did was alter some internal state of the UIView [myView setNeedsDisplay]. This change is completely invisible to Cocoa-Touch. This change is not one of the criteria that warrents a redraw - according to Cocoa-Touch - thus my UIView sits there, unchanged.
This is very, very, very frustrating. I have hit a wall here.
Could someone please suggest a technique, a trick, a hack, that will prompt my UIView to re-render.
Cheers,
Doug
[myView setNeedsDisplay] should cause drawRect: to be sent to myView the next time you're back in the run loop and myView is visible. If changing the bounds is causing the view to redraw (myView.contentMode is UIViewContentModeRedraw), then setNeedsDisplay must be working, since that's how the redraw is signalled by the bounds change. See the UIView class reference for details.
Is your drawRect: being invoked the first time the view is shown? If not, it may be misspelled, overridden in a subclass, or even on the wrong object.
Is the view visible and on screen? It won't be drawn if it is off screen.
Is control returning to the event loop? drawRect: won't be invoked if your application is busy.
Related
I have an NSView subclass foo which overrides drawRect:
- (void)drawRect:(NSRect)dirtyRect
{
[self doStuff];
}
I would have expected that whenever I added an instance bar of my class as a subview of another view, bar would be redrawn. However, when I add bar as the contentView of the main window in my application, I'm not seeing this to be the case.
I tried adding another override:
- (void)viewDidMoveToSuperview
{
self.needsDisplay = YES;
}
...in hopes that when bar gained a superview, it would invalidate its display and force a redraw. No such luck (viewDidMoveToSuperview is being called, but not drawRect:).
I think I may be misunderstanding how drawRect: gets called, and I admit that I don't have the best understanding of the drawing event loop in general. I couldn't find a lot of documentation online about this.
So my question is two-fold: 1) What am I doing wrong in regards to getting drawRect: called? 2) Is there any relevant online documentation regarding the event loop which draws UI in cocoa?
EDIT:
In my UI chain, the instance of foo which isn't getting drawRect: called on it has two subviews: one which is essentially a subclass of NSTextView which sizes to its content, and another which is an NSScrollView. By sheer force of luck, I happened to comment out the code which added the NSScrollView, and suddenly the instance of foo had drawRect called on it again.
So somehow my NSScrollview is keeping my foo from receiving drawRect: . From a quick Google search, I found that other people have had reported problems related to drawing and NSScrollView. However, most of those posts seemed related to subviews within NSScrollView, rather than superviews of NSScrollView.
So here's my newest question: what about my instance of NSScrollView is preventing its parent from receiving drawRect:?
Did you try to invoke [view display]; instead of needsDiplay ?
I have implemented a UIWebView object into a custom UIView (TSAlertView). My UIWebView sits in the middle of the UIView, as a subview.
When I rotate the iOS device screen, the UIWebView object redraws right on top of the old one, without deleting the old one. All the other elements of this custom UIView are destroyed and redrawn when the screen is rotated.
I imagine I must have missed some sort of procedure for deallocating resources or removing the UIWebView from view. I have tried adding 'autorelease' to the declaration of the UIWebView, but to no avail. I wonder if this is a common symptom of a simple oversight I have made in the way the UIWebView is created?
This seems to be a simple case of not telling the UIWebView object it should destroy itself before it is redrawn on each rotate - but I don't know how I can go about this...
Any wisdom gratefully received.
Ideally while rotating you should not create the views again. Instead you can rearrange the frame in delegates appropriately. In your case you might be creating a UIWebView again, which should be avoided. Instead of that, you can keep it as a class level param(Declare in .h file) and adjust the frame in delegate methods for rotation. Also please note that release/autorelease wont remove your view from superview. You need to call removeFromSuperview method to achieve that.
Check this for view resizing and layout handling
I'm struggling with setNeedsDisplay. I thought it was supposed to trigger calls of drawRect: for the view for which it is called and the hierarchy below that if it's within the view's bounds, but I'm not finding that to be the case. Here is my setup:
From the application delegate, I create a view whose size is a square that covers essentially the whole screen real estate. This view is called TrollCalendarView. There is not much that happens with TrollCalendarView except for a rotation triggered by the compass.
There are 7 subviews of TrollCalendarView called PlatformView intended to contain 2D draw objects arranged around the center of TrollCalendarView in a 7-sided arrangement. So when the iPad is rotated, these 7 views rotate such that they are always oriented with the cardinal directions.
Each of the PlatformView subviews contains 3 subviews called Tower. Each tower contains 2D draw objects implemented in drawRect:.
So, in summary, I have TrollCalendarView with empty drawRect:, and subviews PlatformView and Platformview -> Tower that each have drawRect implementations. Additionally, Tower lies within the bounds of Platform, and Platform lies within the bounds of TrollCalendarView.
In TrollCalendarView I've added a swipe recognizer. When I swipe happens, a property is updated, and I call [self setNeedsDisplay] but nothing seems to happen. I added NSLog entries to drawRect: method in each of these views, and only the TrollCalendarView drawRect: method is called. Ironically, that is the one view whose drawRect method will be empty.
There is no xib file.
What do I need to do to ensure the drawRect method in the other subviews is called? Is there documentation somewhere that describes all the nuances that could affect this?
I'm struggling with setNeedsDisplay. I thought it was supposed to trigger calls of drawRect for the view for which it is called and the hierarchy below that if it's within the view's bounds
No, that is not the case. Where did you get that idea?
-setNeedsDisplay: applies only to the view to which it is sent. If you need to invalidate other views, you need to add some code to send -setNeedsDisplay: to them, too. That's all there is to it.
I think this is an optimization in the framework; if your subviews don't need to draw again, then this is a major performance improvement. Realize that almost anything animatable does not require drawrect (moving, scaling, etc).
If you know that all of your subviews should be redrawn (and not simply moved), then override setNeedsDisplay in your main view and do like this:
-(void) setNeedsDisplay {
[self.subviews makeObjectsPerformSelector:#selector(setNeedsDisplay)];
[super setNeedsDisplay];
}
I have tested this, and it causes all subviews to be redrawn as well. Please note that you will earn efficiency karma points if you somehow filter your subviews and make sure you only send that to subviews which actually need redrawn... and even more if you can figure out how not to need to redraw them. :-)
It seems that calling [self setNeedsDisplay] on a UIView does not result in a
[self.layer setNeedsDisplay]. Is it alright if I override setNeedsDisplay as follows:
- (void)setNeedsDisplay {
[super setNeedsDisplay];
[self.layer setNeedsDisplay];
}
Will this get me into trouble? Perhaps there is a better way?
Thank You
I posted this as a comment to another answer, but this may be helpful to other people.
A UIView only calls setNeedsDisplay on its underlying CALayer when drawRect: is implemented (even if the method is empty and does nothing). This tells UIView's setNeedsDisplay that its layer also has some drawing to be done.
Another way to get the layer's setNeedsDisplay to be called automatically is to set the layer's flag needsDisplayOnBoundsChange to true, which will trigger a setNeedsDisplay whenever the view's bounds change (which automatically changes the layer's bounds). But to answer your questions, what you're doing is fine if you didn't override drawRect:, otherwise it's redundant.
I don't see why it would get you into trouble. setNeedsDisplay / setNeedsLayout just flag that the view/layer needs to be displayed / laid out. It'll do the drawing next time around the run loop.
I'm fairly surprised though that calling it on the view doesn't cause it to happen on the layer, because after all a UIView and its associated CALayer are quite tightly integrated.
So overall, yeh if you're finding it's not working for you by just calling setNeedsDisplay on the view then go for your solution. I don't see how it can harm anything.
I am trying to detect touches, but the touchesBegan method is not being called.
In my ViewController, I have added the touchesBegan method. My Nib files owner is set to the correct V.C. The Nib itself consists of the view, with a scroll view and a tab bar. Nested in the scroll view is an image view, which has user interaction enabled. What is precluding touches from being registered, or preventing my implementation of touchesBegan from being called?
I've scoured the Internet and Apple docs, and I can't see what I am doing wrong. Also, I'm not really sure what code I can post here to help with my query. Thanks.
Okay, after a lot more reading, I've now got a scrollview and a imageview, both of which are created programatically. The imageview is a sub view of the scrollview, and scrollview has been subclassed so that the touches ended method can decide whether it was a single touch, in which case call the touches ended method from the view controller, otherwise call its supers method. This works just fine, however, why is it that this cannot be done without subclassing scrollview? Is it my lack of understanding of how scrollview works, or is it just a limitation of it?