在init之后在视图控制器中加载/刷新新数据

时间:2012-04-04 23:52:59

标签: iphone refresh init

我有一个包含建筑信息(字符串)的Building类。当我按下我的DetailViewController(而不是从tableviewcontroller)时,我用它推动建筑物的selcyed并加载正确的信息。我想要做的是在详细视图上有一个按钮,它在下一个构建实例中加载。我尝试了以下但没有运气 - 所有数据都为空/空。我不能正确连接到我的建筑物阵列。

在我的mainViewController

-(IBAction)pushDetailsVC:(id)sender {

    DetailViewController* controller = [[DetailViewController alloc] init]; 
    // Pass the Building object to the controller
    building = [self.buildings objectAtIndex:nsu_selBldg];
    controller.building = building;
    NSLog(@"Building name is now: %@", building);
    [self presentModalViewController:controller animated:YES];
    [controller.uib_backButton addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
    [controller release];
}

在我的DetailViewController中 // h有

@property (nonatomic, retain) Building *building;
@property (nonatomic, retain) PhotoViewerViewController *photoVC;

// m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // This works as expected - the data loads in great.
    self.uill_bldgName.text = self.building.name;
    self.uitt_bldgInfo.text = self.building.bldgInfo;
    self.uiivv_bldgPlan.image = [UIImage imageNamed:self.building.bldgPlan];
    self.uiivv_bldgImage.image = [UIImage imageNamed:self.building.bldgImageZoom];
    NSLog(@"first name%@",self.building.bldgImageZoom);
}

-(IBAction)nextBldg {
    // This does not work - data goes null. PhotoVC is my main view controller and buildings is the array of buildings
    building = [photoVC.buildings objectAtIndex:2];
    Building *building = building;
    NSLog(@"building %@",building);
    NSLog(@"building %@",[photoVC.buildings objectAtIndex:2]);  
    self.uill_bldgName.text = self.building.name;
    self.uitt_bldgInfo.text = self.building.bldgInfo;
    self.uiivv_bldgPlan.image = [UIImage imageNamed:self.building.bldgPlan];
    self.uiivv_bldgImage.image = [UIImage imageNamed:self.building.bldgImageZoom];
    NSLog(@"secon name %@",self.building.bldgImageZoom);    
}

1 个答案:

答案 0 :(得分:1)

我看不到你在哪里初始化photoVC。如果没有初始化,您肯定没有建筑数据。

更好的方法是将构建数组(以及当前索引nsu_selBldg)传递给DetailViewController。然后它可以访问它想要的任何建筑物。

// DetailVC.h

@property (nonatomic, retain) NSArray *buildings;
@property (nonatomic, assign) NSUInteger currentIndex;

// DetailVC.m

Building *currentBuilding = [self.buildings objectAtIndex:self.currentIndex];
NSUInteger nextIndex = (self.currentIndex == self.buildings.count-1)? 0 : self.currentIndex+1;
Building *nextBuilding = [self.buildings objectAtIndex:nextIndex];