从 segue 接收数据后,View Controller 不更新

View Controller does not update after receiving data from a segue

iOS Objective-C 问题:我无法让我的视图控制器显示我从另一个视图控制器传入的值。我怎样才能解决这个问题?

这是交易-

我有 4 个 UILabel 属性(planetTitle、planetSubtitle、distanceFromEarth 和 distanceFromSun),每当我尝试在 ViewDidLoad 方法中更改它们时,它们都会正常运行。示例:我试过

planetTitle.text = @"Test"

而且效果很好。但是,当我尝试在用于接收数据的方法(称为 cellWasPressed)中更改这些属性的文本值时,它不起作用。我已经使用断点测试了我的 cellWasPressed 方法,所有数据都很好 - 我只是无法更改 属性 值。这可能是我错过的非常基本的东西,但请让我知道我哪里出错了。我的 ViewController.m 文件在下面(有点像哈哈)。 (里面的updateViews方法只是想尝试另一种方法,也没有用)。

- (void)viewDidLoad {
   [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
}

-(void) cellWasPressed:(Planet *) planet{

   NSString *planetName = planet.planetName;
   NSString *distanceFromSun = [NSString stringWithFormat:@"%.2f Million Miles",planet.distanceFromSun];
   NSString *distanceFromEarth = [NSString stringWithFormat:@"%.2f Million Miles",planet.distanceFromEarth];
   NSString *planetSubtitle = planet.planetDescription;
   [self updateViews:planetName sun:distanceFromSun earth:distanceFromEarth subtitle:planetSubtitle];
}


-(void) updateViews:(NSString *) title sun: (NSString *) sun earth: (NSString *) earth subtitle: (NSString *) subtitle{
   _planetTitle.text = title;
   _planetSubtitle.text = subtitle;
   _distanceFromSun.text = sun;
}

Segue:(我还将星球 属性 添加到目的地的 .h 文件中)

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    PlanetInformationViewController *planetInformationViewController = segue.destinationViewController;

    UITableViewCell *selectedCell = (UITableViewCell *) sender;

    if([selectedCell.textLabel.text isEqualToString:@"Mars"]){
        planetInformationViewController.Planet = _planetList[0];
        [planetInformationViewController cellWasPressed:_planetList[0]];
    }
    else if ([selectedCell.textLabel.text isEqualToString:@"Earth"]){
        planetInformationViewController.Planet = _planetList[1];
        [planetInformationViewController cellWasPressed:_planetList[1]];
    }
    else if ([selectedCell.textLabel.text isEqualToString:@"Jupiter"]){
        planetInformationViewController.Planet = _planetList[2];
        [planetInformationViewController cellWasPressed:_planetList[2]];
    }

}

在prepare for segue的最后,添加这样的代码。

Swift

var _ = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("loadData"), userInfo: nil, repeats: false)

func loadData()
{
    //Call your cell pressed function here.
}

这将增加通话延迟。

在类似的情况下它对我有用。

UPDATE

objective-c

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(loadData) userInfo:nil repeats:YES];

- (void)loadData:(NSTimer*)timer { 
    //Call your cell pressed function here.
}

当您调用 prepareForSegue 中影响插座的方法时,它不起作用,因为此时插座尚未连接。

  • 在目标视图控制器中创建一个属性

    @property Planet *planet;
    
  • prepareForSegue

    中将行星实例分配给这个属性
    planetInformationViewController.planet = _planetList[x];
    

    顺便说一句:在行星和 _planetList 之间找到一个基于索引的关系,以避免重复代码并且不要从单元格(视图)中获取行星的名称。从数据源数组(模型)中获取。

  • viewWillAppear中调用[self cellWasPressed]并移除方法中的参数

    -(void) cellWasPressed {...