2012-08-13 63 views
0

我創建了一個視圖,該視圖顯示丟失的連接消息給彈出當前視圖的用戶。我想根據連接狀態的變化定期更新視圖。爲什麼UILabel沒有更新?

我可以正確獲取視圖並更改標籤的文本(使用WriteLines進行驗證),但實際顯示中沒有任何更改。我甚至嘗試刪除視圖並讀取並調用SetNeedsDisplay,但似乎沒有任何幫助。

我有一個名爲概覽全局變量:

public static UIView OverView; 

我創建標籤子視圖,將其添加到概覽並彈出概述在當前視圖的面前:

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

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

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

然後在稍後的時間,我嘗試從另一個函數修改它是這樣的:

 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"); 
          }); 
         } 
        } 
       } 
      } 
     } 

回答

0

您是否嘗試過使用在標籤上委託方法,並在發生事件時更改它們的值?

例如,如果你的事件被點擊一個按鈕,你應該有這樣的事情:

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

當您查看加載後,你會看到「初始化」和你的按鈕,一旦你點擊它,標籤文字改變了。

Xamarin對事件和代表方法here有一些解釋。

我希望有所幫助。

+0

我不確定你的意思。你可以請張貼一些代碼片段,以顯示如何可以與我的代碼上面的工作?謝謝。 – nbonwit 2012-08-14 13:07:48

+0

我編輯我的答案是更完整的。委託方法有很多種類,它取決於您使用的不同對象。 – Gnial0id 2012-08-14 14:24:44

+0

我明白了。我真的不知道這將如何幫助我......問題是我可以更改文本(如WriteLines所示),但更新後的文本永遠不會顯示在屏幕上,即使我正在更新文本主線程。 – nbonwit 2012-08-14 18:04:50