0

The view controller's class:

namespace DrawOverride
    {
        public partial class DrawOverrideViewController : UIViewController
        {
            float W, H;
            CGPoint initialPoint;
            Drawer dr;

            public DrawOverrideViewController (IntPtr handle) : base (handle)
            {
            }

            public DrawOverrideViewController ()
            {

            }

            public override void DidReceiveMemoryWarning ()
            {
                // Releases the view if it doesn't have a superview.
                base.DidReceiveMemoryWarning ();

                // Release any cached data, images, etc that aren't in use.
            }

            #region View lifecycle

            public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();
                initialPoint = new CGPoint(5, 5);
                dr = new Drawer (initialPoint);
                **Console.WriteLine("1 - "+initialPoint);**
                // Perform any additional setup after loading the view, typically from a nib.
            }

            public override void ViewWillAppear (bool animated)
            {
                base.ViewWillAppear (animated);
            }

            public override void ViewDidAppear (bool animated)
            {
                base.ViewDidAppear (animated);
            }

            public override void ViewWillDisappear (bool animated)
            {
                base.ViewWillDisappear (animated);
            }

            public override void ViewDidDisappear (bool animated)
            {
                base.ViewDidDisappear (animated);
            }

            #endregion

            public override void TouchesBegan (NSSet touches, UIEvent evt)
            {
                base.TouchesBegan (touches, evt);
                UITouch touch = touches.AnyObject as UITouch;
        }
    }

The second class:

namespace DrawOverride
{
    partial class Drawer : UIView
    {
        CGPath pathed;

        CGPoint initialPoint;
        CGPoint latestPoint;

        public Drawer (IntPtr handle) : base (handle)
        {
        }

        public Drawer (CGPoint a)
        {
            initialPoint = a;
            **Console.WriteLine ("2 - "+a);**
        }

        public override void TouchesBegan (NSSet touches, UIEvent evt)
        {
            base.TouchesBegan (touches, evt);
        }

        public override void TouchesMoved (NSSet touches, UIEvent evt)
        {
            base.TouchesMoved (touches, evt);
            UITouch touch = touches.AnyObject as UITouch;
            latestPoint = touch.LocationInView (this);
            SetNeedsDisplay ();
        }

        public override void TouchesEnded (NSSet touches, UIEvent evt)
        {
            base.TouchesMoved (touches, evt);
        }

        public override void Draw (CGRect rect)
        {
            base.Draw (rect);
            pathed = new CGPath ();

            using (CGContext g = UIGraphics.GetCurrentContext ()) {
                g.SetLineWidth (8);
                UIColor.Blue.SetFill ();
                UIColor.White.SetStroke ();
                **Console.WriteLine("3 - "+initialPoint);**
                if (pathed.IsEmpty) {
                    pathed.AddLines (new CGPoint []{ initialPoint, latestPoint });
                } else {
                    pathed.AddLineToPoint (latestPoint);
                }

                //add geometry to graphics context and draw it
                g.AddPath (pathed);
                g.DrawPath (CGPathDrawingMode.FillStroke);
            }
        }
    }
}

At "Console.WriteLine" 1 and 2 the variable still have a value, (5, 5), like I did it. At the 3, inside the overriden draw method, the value's print is {0,0}. I've tried too use:

Drawer dr = new Drawer(); dr.initialPoint = initialPoint;

But it isn't works too..

Teask Nick
  • 69
  • 7
  • There's nothing in the code you posted that would explain why, if you pass one value to the constructor, the field would later have some other value. A couple of suggestions: if you expect for the value to never change, declare it as `readonly`. If you get a compiler error, then you will know where you're changing it. If you don't, then it means at the `3` you have a completely different instance of the `Drawer` class (those are really the only two possibilities to explain your problem). You might also consider not copying the value from controller to view; pick one place, always keep it there – Peter Duniho Jan 06 '15 at 18:53
  • If you aren't able to solve the issue with the above, then you need to improve the question. See https://stackoverflow.com/help/mcve – Peter Duniho Jan 06 '15 at 18:53
  • No, that's it.. I've done this several times and I never got any errors. Actually, it's not an error. The value just is erased. I think it's a compiler bug. I will try to create another solution and test it. – Teask Nick Jan 07 '15 at 09:38
  • I understand you get no error now. My point is that by adding `readonly`, you can get the compiler to help you find _your bug_, by emitting an error at the location where you do overwrite the value (i.e. in the part of the code you didn't include here). It is nearly certain that this is _not_ a compiler bug. Those are incredibly rare, and would not involve a simple scenario like this. – Peter Duniho Jan 07 '15 at 16:39

0 Answers0