2012-08-26 122 views
1

所以我最近決定加入iOS開發,主要是因爲我喜歡在多個平臺上擴展我的知識,而且因爲我已經瞭解Java並編寫了一個Android應用程序,所以iOS是下一步。MonoTouch:按鈕不可見

所以我的問題是這樣的: 在一個表中,我使用一個自定義的tablesource來響應點擊一個新的視圖被加載的行。現在在這個視圖中,我想添加一個返回按鈕來返回到前一個視圖。

PointF locationOne = new PointF (5, 5); 
    PointF locationTwo = new PointF (5, 100); 
    SizeF size = new SizeF (120, 40); 

    RectangleF tView = new RectangleF (locationOne, size); 
    RectangleF tView2 = new RectangleF (locationTwo, size); 
     UILabel l1 = new UILabel(){Text = "LOOL",Frame=tView}; 

     UIButton backbutton = new UIButton(){ 
      Frame=tView2 
     }; 
     backbutton.Frame = tView2; 
     backbutton.TitleLabel.Text = "Zurueck"; 
     backbutton.Hidden = false; 

     UIView previousview = viewController.View; 
     backbutton.TouchDown += (sender, e) => { 
      viewController.View = previousview; 
     }; 

     UIView v = new UIView(); 
     v.AddSubview(l1); 
     v.AddSubview (backbutton); 


     viewController.View = v; 

這將是我現在的代碼。標籤正在顯示,按鈕不是。有什麼我失蹤?

由於已經:)

+0

您應該使用UINavigationController,因爲它帶有後退按鈕。 – Candide

+0

瀏覽視圖之間,你應該看看使用UINavigationController而不是創建自定義導航 – Jason

回答

1

UIViewv沒有框架。您需要將其設置爲父控制器的框架(可能是v.Frame = viewController.View.Frame;)。然後,換出viewcontroller的視圖。

編輯:

UIButton的初始化是不正確的。以下是如何創建一個常規按鈕:

PointF locationOne = new PointF (5, 5); 
PointF locationTwo = new PointF (5, 100); 
SizeF size = new SizeF (120, 40); 

RectangleF tView = new RectangleF (locationOne, size); 
RectangleF tView2 = new RectangleF (locationTwo, size); 
UILabel l1 = new UILabel(){Text = "LOOL",Frame=tView}; 

UIButton backbutton = UIButton.FromType(UIButtonType.RoundedRect); 
backbutton.Frame = tView2; 
backbutton.SetTitle("Zurueck", UIControlState.Normal); 

UIView previousview = viewController.View; 
backbutton.TouchUpInside += (sender, e) => { 
    viewController.View = previousview; 
}; 

UIView v = new UIView(); 
v.BackgroundColor = UIColor.White; 
v.AddSubview (l1); 
v.AddSubview (backbutton); 
viewController.View = v; 
+0

沒有使按鈕出現 –

+0

Volker看到我更新的答案。他們的按鈕初始化的方式是不正確的。這應該工作。 – Candide

+0

完美的作品。非常感謝你 :) –