2011-05-06 31 views
2

使用monotouch和monodevelop,我想創建一個自定義控件。 我跟着這個步驟:綁定通過單點觸摸在界面生成器中創建爲UIView的自定義控件

  • 添加新的文件爲 「iPhone視圖」(稱之爲TestView)在Interface Builder
  • 編輯TestView.xib,ES。改變它的背景和大小
  • 在Interface Builder中編輯MainWindow.xib,添加一個UIView並將它的類標識設置爲「TestView」。

在這一點上,我想啓動應用程序,並在MainWindow的UIView中看到TestView的一個實例的內容。

爲了得到這個「綁定」,我嘗試了幾個步驟(我知道它可以通過代碼創建插座等來完成,但我想了解它是否可以通過Interface Builder完成)。

在其中一個嘗試的方法中,我通過Interface Builder將TestView作爲類標識符設置在TestView.xib視圖中:通過這種方式,MonoTouch在TestView.xib.designer.cs中創建了代碼。

[MonoTouch.Foundation.Register("TestView7")] 
public partial class TestView7 { 
} 

然後,在TestView.xib.cs,我說:

public partial class TestView : UIView 
{ 
    public TestView (IntPtr p) : base(p) 
    { 
    }  
} 

當我啓動應用程序,我不能在主窗口TestView的內容的視圖中看到的,但如果在TestView構造我添加如

BackgroundColor = UIColor.Yellow;

然後,我可以在MainWindow中看到TestView ...或更好的我只能看到黃色的矩形,而不是真正的內容!

所以,問題是TestView綁定到MainWindow中的UIView,但它的內容只來自代碼,而不是來自通過TestView.xib中的Interface Builder定義的內容。

有沒有辦法從TestView.xib加載內容?

回答

0

看看http://ios.xamarin.com/Documentation/API_Design#Interface_Builder_Outlets_and_C.23

我相信你需要做的是添加處理筆尖文件構造(以下是從上面的鏈接):

當使用MonoTouch的應用程序就需要創建一個從的UIViewController派生的類並實現它是這樣的:

public class MyViewController : UIViewController { 
    public MyViewController (string nibName, NSBundle bundle) : base (nibName, bundle) 
    { 
    // You can have as many arguments as you want, but you need to call 
    // the base constructor with the provided nibName and bundle. 
    } 
} 

然後從護理文件加載您的視圖控制器,你這樣做:

var controller = new MyViewController ("HelloWorld", NSBundle.MainBundle, this); 

這會從NIB加載用戶界面。

相關問題