2

After having spent the whole day looking for a solution I feel only more confused and upset.

Let's face the problem:

I'm developing a single view iOS app made up of an AppDelegate (of course..), A ViewController and a "DrawingClass" (subclass of UIView).

In the main.storyboard i can see my mainViewControllerScene, and inside this main view directed by the viewController, I have inserted a UIView object from the palette in the interface builder and set it to be controlled by my "Drawing class" because I need to use the DrawRect method to draw custom lines.

Well, when starting the app, the defaults lines in my "DrawingClass" are being drawn, so drawrect is being called.


But when, after having pressed a button linked to an IBAction, I try to call again drawrect through setNeedsDisplay or anything it doesn't work.


Let's be more clear:

-I'm sure that the view controlled by "DrawingClass" is being drawn correctly on startup

-I'm sure that the IBAction is called (I used an NSLog)

-I can't figure out how to redraw that view. (The one controlled by "DrawingClass")

In the viewController I tried both [self.view setNeedsDisplay] and [myView setNeedDisplay] but none of them called my drawrect method in the "DrawingClass"

What I'm doing wrong? Am I forgetting to init something ? I tried even to call those methods on the main thread but nothing.

I think this question could help many so please ask if you need something more to work out this problem.

Thank you so much.

Alberto
  • 4,212
  • 5
  • 22
  • 36
  • So to get this straight, you are drawing lines on the view at startup and then when a user clicks the button, an IBAction is triggered that draws the same lines again or something? – valheru Jan 04 '14 at 18:53
  • Yeah an IBAction is triggered.. To simplify imagine that the lines at startup represent a square figure because a variable in "DrawingClass" is set to be equal to "4", then when I press the button ,I want to change that variable to "3" and draw a triangle. I'm able to change the variable by creating an instance of my DrawingClass in the ViewController and calling a method, but I'm not able to refresh the view (call drawrect).. I have been enough clear ? – Alberto Jan 04 '14 at 19:07
  • ok. want to post your IBAction method? – valheru Jan 04 '14 at 19:09
  • - (IBAction)redraw:(id)sender { DrawingClass *DC = [[DrawerClass alloc] init]; //Simplified method that sets the global variable in DrawingClass from 4 to 3 and works (Checked with NSLog) [DC updateVariable:3] – Alberto Jan 04 '14 at 19:22
  • //Now that my variable is set to 3 i want to redraw my Controller View or the DrawingClass view in order to invoke DrawRect but is not working (I'm sure because i have an NSLog in drawrect). [self.view setNeedsDisplay] //Trying to refresh controllerView [drawingView setNeedsDisplay] //Trying to refresh the view set to be controlled by my Drawing class. – Alberto Jan 04 '14 at 19:23
  • //None of this solution works.. How can i force the view to redraw ? //Outlets are linked.. } – Alberto Jan 04 '14 at 19:23
  • So you have a DrawingClass in your view (as a subview?) But then in your IBAction you're allocating a brand new one? This one would be different than the one that's already in the view. You need to either add this updated DC as a subview or get a reference to the existing DC before drawing again. – valheru Jan 04 '14 at 19:24
  • Ok but let's say I just want to refresh my view like: -(IBAction)redraw:(id)sender { //How to refresh the view in order to call the drawrect method inside Drawing class ? } – Alberto Jan 04 '14 at 19:27
  • Let's skip the variable change so i don't need any instance.. Just want to refresh.. It still does not work and i can't figure out why Sorry for the mess, if needed i will provide you a short video to demonstrate what i mean. – Alberto Jan 04 '14 at 19:27
  • Can you post your code (in the original question under edit) for your ViewController *.h/*.m files? – valheru Jan 04 '14 at 19:35
  • Can you log the values of `self.view`, `[self.view subviews]`, and `myView` in both your `viewDidLoad` and in your `redraw` methods? Knowing whether the object addresses match or not could help narrow it down. – Phillip Mills Jan 04 '14 at 19:42
  • 1
    You should use setNeedsDisplay method to request that a view be redrawn only when the content or appearance of the view change. If you simply change the geometry of the view, the view is typically not redrawn. Instead, its existing content is adjusted based on the value in the view’s contentMode property. – ldindu Jan 04 '14 at 19:46
  • I'm uploading a demonstrative video.. Phillip: 2014-01-04 20:53:11.782 test[4102:70b] > – Alberto Jan 04 '14 at 19:54
  • 2014-01-04 20:53:11.783 test[4102:70b] ( ">", ">", "<_UILayoutGuide: 0x109519d20; frame = (0 0; 0 20); hidden = YES; layer = >", "<_UILayoutGuide: 0x10951a7e0; frame = (0 568; 0 0); hidden = YES; layer = >" ) – Alberto Jan 04 '14 at 19:54
  • look here: https://www.dropbox.com/s/pknr5azeqb0jrmc/Xcode.mp4 – Alberto Jan 04 '14 at 19:55
  • You will probably have to download the file to see 720p – Alberto Jan 04 '14 at 19:56

0 Answers0