简体   繁体   中英

Multiple instance UIView only load xib file in the first instance

I'm working on as slideshow with some CustomUIView showed in a UIScrollView and it works good. But i have a custom UIView to show the content of each news. My slide works goods , but the XIB file of my custom view is loaded only for the first one and other customview are empty.

Here is my code :

my 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

here the result : EDITED : i change the color of the view in my custom view. and we can see that only the first one is realy loaded with xib file. and i dont know why !!!! can you help me?

在此处输入图片说明

在此处输入图片说明

i solve my problem by adding :

  self.frame = frame; 

to my customView.m initWithFrame

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM