簡體   English   中英

多實例UIView僅在第一個實例中加載xib文件

[英]Multiple instance UIView only load xib file in the first instance

我正在做幻燈片演示,其中在UIScrollView顯示了一些CustomUIView ,並且效果很好。 但是我有一個自定義的UIView來顯示每個新聞的內容。 我的幻燈片可以工作,但是我的自定義視圖的XIB文件僅在第一個視圖加載,而其他customview是空的。

這是我的代碼:

我的ViewController.m

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];

    int nbrNews = _sharedDataManager.rssfeeds.count;
    _sliderView.contentSize = CGSizeMake(self.view.frame.size.width*nbrNews, self.view.frame.size.height);

    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = nbrNews;

    for (int i = 0; i< nbrNews; i++) {
         NewsView *newsView = [[NewsView alloc] initWithFrame:CGRectMake(i*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];


        NSDictionary *news = [_sharedDataManager.rssfeeds objectAtIndex:i];
        newsView.titleLabel.text = [news objectForKey:@"title"];

        [_sliderView addSubview:newsView];

    }

}

CustomView.h

#import <UIKit/UIKit.h>

@interface NewsView : UIView

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;


@end

CustomView.m

#import "NewsView.h"

@implementation NewsView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"OneNewsView" owner:self options:nil] objectAtIndex:0];
    }
    return self;
}

@end

在這里結果:編輯:我在自定義視圖中更改視圖的顏色。 並且我們可以看到只有第一個確實加載了xib文件。 而且我不知道為什么! 你能幫助我嗎?

在此處輸入圖片說明

在此處輸入圖片說明

我通過添加解決我的問題:

  self.frame = frame; 

到我的customView.m initWithFrame

暫無
暫無

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

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