我的视图中有两个容器视图,试图模仿左侧的拆分视图控制器tableview和右侧的详细视图。在主人didselect
上使用tableview
行时,详情视图应显示一些详细信息
问题是当我将数据传递给详细视图控制器实例时,它总是返回null。
关于主人:
@property (nonatomic,strong) LogDetailViewController *wods;
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:nil];
self.wods= [sb instantiateViewControllerWithIdentifier:@"LogDetailViewController"];
//also tried self.wods= [[LogDetailViewController alloc] init];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//one of the if else method rest of it was too long
else if ([indexPath section]==1){
GIRLS *girls =[self.girlsList objectAtIndex:indexPath.row];
self.wods.girls = girls;
self.wods.section=1;
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshLOGDetails" object:nil];
}
}
详细说明:
@property int section;
@property (nonatomic, strong) GIRLS *girls;
//refresh notification
extern NSString * const NOTIF_refreshLOGDetails;
@synthesize section;
@synthesize girls;
- (void)refreshLOGDetails:(NSNotification *)notif
{
NSLog(@"Notification refreshLOGDetails works");
[self configureView];
}
- (void)configureView
{
//also tried self.girls and self.section
NSLog(@"Section is %i",section);
NSLog(@"configure view object %@",girls);
}
输出:
Notification refreshLOGDetails works
Section is 0
configure view object (null)
我做错了什么?为什么我得到空值?
答案 0 :(得分:0)
我认为问题在于,当您实例化或使用alloc init时,您正在创建LogDetailViewController的新实例。如果您在故事板中的容器视图中将它们设置为控制器,则需要引用在那里创建的控制器,而不是创建新的控制器。您可以使用self.parentViewController来获取对控制器的引用,其中包含容器视图。 self.parentViewController.childViewControllers将为您提供一个应该包含master和detail的数组 - 您只需要从该数组中获取正确的数组并将其返回。