0

I have created a view that shows lost connection messages to users which pops over the current view. I want to update the view periodically based on connection status changes.

I can properly get the view and change the text of a label (verified with WriteLines), but nothing changes on the actual display. I even tried removing the view and readding it and calling SetNeedsDisplay, but nothing seems to help.

I have a global variable called OverView:

public static UIView OverView;

I create the label subview, add it to the overview and pop the overview in front of the current view:

            UILabel labelTitle = new UILabel();
            labelTitle.Text = title;

            UIView labelTitleView = (UIView) labelTitle;
            labelTitleView.Tag = 5000;
            OverView.AddSubview(labelTitleView);

            curView.InsertSubviewAbove(OverView, curView);
            curView.BringSubviewToFront(OverView);

And then at a later time, I try to modify it like this from another function:

        if ((OverView != null) && (OverView.Subviews != null))
        {
            for (int i = 0; i < OverView.Subviews.Length; i++)
            {
                WriteToConsole("Type: " + OverView.Subviews[i].GetType());

                if (OverView.Subviews[i] is UILabel) 
                {
                    WriteToConsole("Found Label with Tag: " + ((UILabel)(OverView.Subviews[i])).Tag + " Text: " + ((UILabel)(OverView.Subviews[i])).Text);

                    if (((UILabel)(OverView.Subviews[i])).Tag == 5000)
                    {
                        WriteToConsole("Setting subview Title to: " + lostConnectionTitle);

                        lock (overViewLocker)
                        {
                            appReference.InvokeOnMainThread(delegate
                            {
                                UILabel tempLabel = ((UILabel)(OverView.Subviews[i]));
                                tempLabel.Text = lostConnectionTitle;
                                OverView.Subviews[i].RemoveFromSuperview();
                                OverView.AddSubview(tempLabel);
                                OverView.BringSubviewToFront(tempLabel);
                                OverView.SetNeedsLayout();
                                OverView.SetNeedsDisplay();
                                WriteToConsole("SetNeedsDisplay");
                            });
                        }
                    }
                }
            }
        }
nbonwit
  • 305
  • 4
  • 12

1 Answers1

0

Have you tried to use delegate methods on your label, and change their value when events occur ?

For example, if your event is clicking on a button, you should have something like that:

yourLabel.Text = "Init";
buttonExample.TouchUpInside += (sender, e) => {
    yourLabel.Text = "I touched my button";
};

When your View loads, you'll see "Init" and your button and once you click on it, the label text changed.

Xamarin has some explanation about events and delegate methods here.

I hope that helped.

Nate B.
  • 942
  • 11
  • 31
  • I'm not really sure what you mean by that. Can you please post some code snippets to show how that could work with my code above? Thanks. – nbonwit Aug 14 '12 at 13:07
  • I edited my answer to be more complete. There are many types of delegate methods, it depends of the different object you use. – Nate B. Aug 14 '12 at 14:24
  • I see. I'm not really sure how that would help me...the issue is that I can change the Text (as shown by WriteLines), but the updated text never gets displayed on the screen, even though I'm updating it on the main thread. – nbonwit Aug 14 '12 at 18:04