簡體   English   中英

在我的故事板中添加帶有xib的自定義uiview

[英]add a custom uiview with xib in my storyboard

我正在使用swift2.0,我剛剛添加了一個名為SortableTableTitleView的自定義UIView

在此處輸入圖片說明

在此處輸入圖片說明

然后將UIView拖到情節UIView中,然后將視圖的類更改為SortableTableTitleView

在此處輸入圖片說明

當我嘗試在自定義視圖中設置標簽的文本時,遇到了一個錯誤的訪問錯誤。

我試圖編寫視圖的initwithcoder方法,但是我不知道如何從xib加載元素。 (與ObjectiveC有所不同)。 那么,我該如何解決呢?

在此處輸入圖片說明

不能使用xib的內部文件Storboard文件。 因此,要么完全在Storyboard創建視圖,然后照常使用它即可。 或者,從xib文件中添加視圖,然后通過代碼將一個subView添加到storyboard subView上的視圖。

所以在viewController中是viewController

UINib *nibFile = [UINib nibWithNibName:@"SortableTableTitleView" bundle:nil];
            NSArray *views = [nibFile instantiateWithOwner:self options:nil];
            UIView *sortableTableTitleView = views[0]; //Assuming it is the only view in the xib file

//Add it where you want as a normal view now: 
[self.view addSubview: sortableTableTitleView];

我意識到上面的代碼在Objective-C中-當我得到差距時我會得到Swift的比較(對Swift和其他都是新手)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM