2013-02-21 66 views
1

我有興趣寫我的自定義視圖,所以我創建了以下XIB文件:如何在UIStoryBoard中加載自定義視圖?

enter image description here

這是定義文件:

- (void)_baseInit { 
    NSLog(@"Unseen View loaded"); 
    [self addSubview:[self activityIndicator]]; 
    [self activityIndicator].alpha = 1.0; 
    [self activityIndicator].frame = CGRectMake(round(([self imageView].frame.size.width - 25)/2), 
               round(([self imageView].frame.size.height - 25)/2), 25, 25); 
    [self activityIndicator].hidesWhenStopped = YES; 
    [self showIndicator]; 

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)]; 
    [self imageView].userInteractionEnabled = YES; 
    [panRecognizer setMinimumNumberOfTouches:1]; 
    [panRecognizer setMaximumNumberOfTouches:1]; 
    [panRecognizer setDelegate:self]; 
    [[self imageView] addGestureRecognizer:panRecognizer]; 

} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self _baseInit]; 
    } 
    return self; 
} 

- (id)initWithCoder:(NSCoder*)coder 
{ 
    if ((self = [super initWithCoder:coder])) { 
     [self _baseInit]; 
    } 
    return self; 
} 

我試圖把它掛在我的故事板:

x

我有我的電話MainViewControllerŤ他的viewDidLoad中:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.unseenView = [[[NSBundle mainBundle] loadNibNamed:@"UnseenView" owner:self options:nil] objectAtIndex:0]; 
    self.unseenView.delegate = self; 

不幸的是,沒有什麼是顯示在我的模擬器幾乎一無所有,甚至沒有文字標籤enter image description here

但是我看到以下日誌消息:

2013-02-20 17:37:58.929 Giordano.iPhone[66857:c07] Unseen View loaded 
2013-02-20 17:37:58.934 Giordano.iPhone[66857:c07] Unseen View loaded 

我在做什麼錯?

回答

1

看起來您正在準備您的視圖,但您並未將其添加到視圖層次結構中。在viewDidLoad代碼,你需要加入這行:

[self.view addSubview:unseenView]; 
+0

你是我的救命恩人。但是,我注意到以下幾點:1.視圖被截斷(可能是由於我的標籤欄項),2.我無法點擊這個新視圖。你知道爲什麼嗎? – disappearedng 2013-02-21 02:19:42

+0

@disappearedng TabBar肯定會截斷視圖。您可能需要在「模擬量度」頁面上的模擬標籤欄添加到xib文件的底部,以查看實際視圖在標籤欄上的顯示效果。假設你從中加載視圖的類(即loadNibNamed'的調用中的'owner:self'參數)是你在nib文件中建立連接的類型,交互應該可以工作。這是[相關討論的鏈接](http://stackoverflow.com/q/3078013/335858)。 – dasblinkenlight 2013-02-21 02:56:20

+0

我刪除了故事板中的UnseenView,我仍然看到添加的子視圖。有無論如何要說:「無論你在故事板上有什麼看法,用這個子視圖替換這個視圖,使得位置和大小匹配?」 – disappearedng 2013-02-21 03:04:23

相關問題