将NSArray从self.view传递到子视图

时间:2016-08-17 13:15:38

标签: ios objective-c nsarray

我想将NSArray从我的self.view传递到子视图这里是我的代码。

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

}

这是我的子视图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

2 个答案:

答案 0 :(得分:2)

首先删除数组的alloc init。  添加一些代码来解决您的问题。

答案 1 :(得分:0)

为什么不在- (instancetype)initWithRssDats:(NSArray *)rssData中提供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];