Vix Hunk Vix Hunk - 3 months ago 23
iOS Question

Pass NSArray from self.view to subview

i want to pass NSArray from my self.view to a subview here is my code.

mainView.h

NSArray *rssData;
@property(nonatomic,retain)NSArray *rssData;


mainView.m

@synthesize rssData;
MySubView *datacarry = [[MySubView alloc] init];
for (int i = 0; i<rssOutputData.count; i++) {

self.rssData = [[NSArray alloc] initWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:[[rssOutputData objectAtIndex:i]xmlhotel_name], @"hotelname",
[[rssOutputData objectAtIndex:i]xmlhotel_price], @"startingfrom",
[[rssOutputData objectAtIndex:i]xmlhotel_city],@"city",
[[rssOutputData objectAtIndex:i]xmlhotel_distance],@"distance",
[[rssOutputData objectAtIndex:i]xmlhotel_image],@"imagesnames",
[[rssOutputData objectAtIndex:i]xmlhotel_stars],@"numberofstars",
[[rssOutputData objectAtIndex:i]xmlhotel_id],@"hotelid",
@"45.5016889",@"hotelLat",
@"-73.567256",@"hotelLong", nil],nil];

datacarry.rssData = self.rssData;
nslog(@"%@",datacarry.rssData); // here i can see data that is is transferred

}


here is my subview which is MySubView

MySubView.h

NSArray *rssData;

@property(nonatomic,retain)NSArray *rssData;


MySubView.m

@synthesize rssData;
//rssData= [[NSArray alloc] init];
nslog(@"%@",rssData); // here i can see NO DATA

Answer

Why not provide an init method like - (instancetype)initWithRssDats:(NSArray *)rssData in MySubView?

MySubView.h

- (instancetype)initWithRssDats:(NSArray *)rssData;

MySubView.m

- (instancetype)initWithRssDats:(NSArray *)rssData {
    self = [super init];
    if (self) {
        self.rssData = [rssData copy];
    }
    return self;
}

mainView.m

MySubView *datacarry = [[MySubView alloc] initWithRssDats:self.rssData];