2016-08-17 110 views
-2

我想從我的self.view中將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 
+0

好了,你不必再頁頭中的init的mySUbView。 –

+0

即使然後我不能在nsSUbView –

+0

嘗試強烈而不是保留nslog數據。你在哪裏在mySUbView中做NSLOg? –

回答

0

爲什麼不在MySubView中提供像- (instancetype)initWithRssDats:(NSArray *)rssData這樣的init方法?

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]; 
2

首先除去陣列的alloc初始化。 放了一些更多的代碼來解決你的問題。

+0

請查看更新後的問題 –

相關問題