我必须对我的代码进行一项调整,并且在使用此解决方案时还需要另一种选择。
在我的例子中,我有一个 导航控制器 在中间打断。 MainViewController Segue 指向那个 Navigation Controller,然后有另一个 Segue 从它指向 SecondViewController 。
因此,如果您需要从 Navigation Controller 获取 ViewController,您可以使用这样的代码:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// ScoreViewController Storyboard Segue
if ([segue.identifier isEqualToString:@"AwesomeSegue"]) {
// Finding ViewController that is needed in Navigation Controller
UINavigationController *navigationController = segue.destinationViewController;
ScoreViewController *scoreViewController = [[navigationController viewControllers] objectAtIndex:0];
// Bellow implement your delegate or any data you want to pass
}
}
代表:
scoreViewController.delegate = self;
传递数据:
scoreViewController.newLabel.text = self.oldLabel.text;
调用方法:
[scoreViewController updateLabelText:self.oldLabel.text];
顺便说一下,传递数据你应该直接从Sugue分配给IBOutlet。
如果您只是传递NSString(或其他任何内容),并尝试将其文本分配给viewDidLoad 中的UILabel,它将是Nill。
这是因为viewDidLoad 在Segue之前实现。
在这里使用viewDidAppear 也无济于事,因为它会在 SecondViewController 呈现到屏幕后加载文本。您可能会看到 UILabel 文本如何从“标签”变为“愚蠢的名称”。
好吧,如果有人有一些建议,我很高兴听到,因为我也只是一个新手。