当我再次推回VC时,为什么IBOutlet标签会消失?

时间:2014-04-16 02:46:22

标签: ios objective-c uitableview

我有一个NavigationController来控制我的VC。 VCA是rootViewController。 VCB是一个带有tableView的viewController。 CustomCell是一个自定义类,我继承自UITableViewCell,内置了一个xib文件,VCB使用CustomCell的委托函数和AFNetworking类如下所示进入单元格:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    NSString *url=[NSString stringWithFormat:@"%@%@",testPath,urlString];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager POST:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        dataList=responseObject;
        NSLog(@"af get data");
        NSLog(@"%@",[[dataList objectAtIndex:0]objectForKey:@"age"]);
        [self.tableViewList reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error:%@",error);
    }];

}
return self;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=@"RecommendTableCellIdentifier";
static BOOL nibsRegistered = NO;
if (!nibsRegistered) {
    UINib *nib = [UINib nibWithNibName:@"RecommendTableCell" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
    nibsRegistered = YES;
}
RecommendTableCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
    cell=[[RecommendTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
dataRow=[dataList objectAtIndex:[indexPath row]];
cell.children=[dataRow objectForKey:@"children"];
if (cell.childrenLabel) {
    NSLog(@"YES");
}else{
    NSLog(@"NO");
}
NSLog(@"cell get data:");
NSLog(@"%@",[dataRow objectForKey:@"age"]);
[cell addContent:dataRow];
[cell.wantToSeeButton setTag:[indexPath row]];
[cell.wantToSeeButton addTarget:self action:@selector(iWantToSeeClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;

}

CustomCell xib有很多IBOutlet标签。

问题是: 当我进入VCB时,单元格数据正常显示。然后我弹回VCA然后再次进入VCB,这次,单元格数据没有显示。 如你所见,我在代码中有很多NSLog检查,所有检查记录正确的事情但cell.childrenLabel记录NO。这似乎是我第二次进入VCB,IBOutlet childrenLabel没有被引入。 有什么问题以及如何解决这个问题?非常感谢!

1 个答案:

答案 0 :(得分:1)

您不应该将nibsRegistered变量设为静态。

一旦将该变量设置为YES,它将在程序的生命周期内保持为YES。当你第二次回到你的视图时,它认为它已经被注册了。

您可以添加属性并使用它,例如:

self.nibsRegistered = YES;

或者,更自然地,在viewDidLoad方法中执行注册代码(每个视图控制器实例只运行一次)。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UINib *nib = [UINib nibWithNibName:@"RecommendTableCell" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
}