问题
I have a VC with a subview. At the subview level
I have a button. I want the button to result in the view being updated.
The IBAction
code is in the VC. All it currently does is toggle a BOOL
value.
Back in the subview drawRect: method is the code that tests the BOOL
and changes the view accordingly.
My observation is that when the button is pressed, the IBAction code runs, toggles the BOOL
, but the drawRect:
method doesn't get called.
I tried calling drawRect manually (not good practice and didn't work). I tried calling setNeedsDisplay but that too didn't work (couldn't figure out how to reference the subview, which is created programmatically).
Suggestions welcome.
IBAction code is trivial but included below:
- (IBAction)rotateWindowButtonWasPressed:(id)sender
{
// just flip the boolean here. Need to make the code change in drawRect: in top2View.m
if ([myGlobals gWinVertical])
[myGlobals setGWinVertical:NO];
else
[myGlobals setGWinVertical:YES];
// Hoping this would force a redraw, but bounds have to change...:(
//_top2ViewOutlet.contentMode = UIViewContentModeRedraw;
}
回答1:
to issue a drawRect call, call setNeedsDisplay
on the view you changed.. the OS will call drawRect on it and affected overlapping views automatically.
in your case I think
[self.view setNeedsDisplay]
回答2:
Not sure what happened - there was an answer above that solved my issue but now it's gone? Anyway - the answer is to create the button programmatically and then I can include the method at the subview level where drawRect: is, and using setNeedsDisplay to repaint the screen. That vs. using IB and having the IBAction code at the VC level.
来源:https://stackoverflow.com/questions/15084270/button-to-trigger-drawrect-to-redraw-a-view