将数据从UITableView传递到XIB子视图

时间:2015-07-02 16:52:59

标签: ios objective-c xcode uitableview subview

我确信有一个简单的解决方案,我环顾四周,找不到任何帮助。我正在尝试将数据从UITableview传递到子视图。我不想使用segue,因为我希望xib文件出现在具有所需数据的同一个viewcontroller上。 NSLog按预期输出,但不传递给“nowplayingnib”视图中的字符串。

ViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

SongInfo *song = songsArray[indexPath.row];



NowPlayingNib *nowplayingnib = (NowPlayingNib *)[NowPlayingNib viewFromNib];


[self.view addSubview:nowplayingnib];


nowplayingnib.videoId = song.videoId;

NSLog(@"%@", song.videoId);

}

更新

NowPlayingNib.h

@interface NowPlayingNib : UIView 

@property (strong, nonatomic) NSString *videoId;
@property (strong, nonatomic) NSString *title;

+(id)viewFromNib;


@end

NowPlaying.m

@implementation NowPlayingNib

@synthesize videoId;

-(void)awakeFromNib{


NSLog(@"video id inside nib = %@", self.videoId);

}

+(id)viewFromNib{
ViewController *songinfo = [[[NSBundle mainBundle] loadNibNamed:@"NowPlayingNib" owner:self options:NULL]lastObject];

return songinfo;


}

2 个答案:

答案 0 :(得分:0)

nowplayingnib.videoId的类型是什么?

将其设为@property (nonatomic,strong)并再次检查

重写init方法,如:

+(id)viewFromNib{
ViewController *songinfo = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:NULL]lastObject];

return songinfo;


}

答案 1 :(得分:0)

您的输出NSLog(@"video id inside nib = %@", self.videoId); 位于awakeFromNib方法中,但您只是在加载nib并将其添加到superview后将值赋给videoId?