![](/img/trans.png)
[英]Xib for custom UIView or UIView with class set to this custom UIView
[英]iOS5 Assign a .xib to a custom UIView class
HI有一個自定義視圖類,使用以下代碼加載並放置在我的主視圖中。 之所以我想用不同的內容填充它,而不是每次在代碼中構建一個視圖,如果我創建一個自定義類,我可以在循環等中重復使用它,我得到這個在代碼中工作得很好,即鋪設出按鈕標簽等
但是,如果我創建一個新的用戶界面視圖,而不是手動編寫代碼,那么在該視圖上可視化地構建我的文本字段,標簽和按鈕。
然后將其連接到我的自定義類。
這是我遇到問題的地方,如何連接這個視圖xib文件,以便在放入我的代碼時它變得可見。 我已將xib文件中的自定義類屬性分配給我的自定義文件,但我還缺少什么?
.h文件:
#import <UIKit/UIKit.h>
@interface blogView : UIView
@end
.m文件:
#import "blogView.h"
@implementation blogView
- (id)init
{
self = [super initWithFrame:CGRectMake(0, 0, 478, 220)];
if (self != nil)
{
NSLog(@"Blog View loaded");
self.backgroundColor = [UIColor yellowColor];
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 400, 40)];
[titleLbl setText:@"This is the Title"];
[self addSubview:titleLbl];
}
return self;
}
@end
我的xib文件名為blogView.xib,它是一個View用戶界面。
在我的主視圖控制器和我有的ViewDidLoad中
blogView *blogItem = [[blogView alloc]init];
[self.view addSubview:blogItem];
當我運行這一切都工作正常,但我想鏈接到.xib文件,以節省時間等謝謝
好好環顧四周,嘗試一些線索和建議,我設法用以下方法做到這一點:
在我的.m文件中,我放置/更改了以下內容:
self = [super init];
if (self != nil)
{
NSArray *theView = [[NSBundle mainBundle] loadNibNamed:@"blogView" owner:self options:nil];
UIView *nv = [theView objectAtIndex:0];
.. rest of code.
[self addSubview:nv];
.. rest of code.
非常感謝
當我重新啟動我的viewcontroller類時,我掙扎了一個小時。 這對我來說在Xcode 5中起了作用
讓我們說在浪費時間之后我非常生氣
您可能希望為視圖創建一個控制器,並使用initWithNibName:bundle加載該視圖:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.