【发布时间】:2011-11-27 13:33:35
【问题描述】:
在两个视图控制器之间实现转场时,如何使用转场对象更改目标视图控制器的属性?文档说这可以在 prepareForSegue: sender: 方法中完成。我试过但没有成功
【问题讨论】:
标签: storyboard ios5
在两个视图控制器之间实现转场时,如何使用转场对象更改目标视图控制器的属性?文档说这可以在 prepareForSegue: sender: 方法中完成。我试过但没有成功
【问题讨论】:
标签: storyboard ios5
我不知道你是否还需要这个问题的答案,但这是一个如此孤独的帖子,如果我是正确的,这不再属于 NDA。如果我弄错了,请将我的回答调整为遗忘,所以我们开始吧:我刚刚完成了一些使用你需要的东西。这是对我有用的代码:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"relevantSegueIdentifier"])
{
// [segue destinationViewController] is read-only, so in order to
// write to that view controller you'll have to locally instantiate
// it here:
ViewController *upcomingViewController = [segue destinationViewController];
// You now have a solid reference to the upcoming / destination view
// controller. Example use: Allocate and initialize some property of
// the destination view controller before you reach it and inject a
// reference to the current view controller into the upcoming one:
upcomingViewController.someProperty = [[SomePropertyClass alloc] initWithString:@"Whatever!"];
upcomingViewController.initialViewController = [segue sourceViewController];
// Or, equivalent, but more straightforward:
//upcomingViewController.initialViewController = self;
}
}
这假定 someProperty 和 initialViewController 都是目标视图控制器的合成访问器。希望这会有所帮助!
【讨论】:
destinationViewController 上的属性没有更新。有什么建议吗?
我编写了一个教程,其中提供了代码示例,用于将信息传递到新场景以及使用委托从场景返回信息,因此请查看它应该可以解决您的问题。 iOS 5 Storyboard: How To use Segues, Scenes and Static Content UITableViews
【讨论】:
我制作了一个关于这个主题的视频。我希望它有所帮助。 http://full.sc/17yKkZF
【讨论】:
我在源视图控制器中使用的是:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *upcomingViewController = [segue destinationViewController];
upcomingViewController.view.tag = [[segue identifier] hash];
}
然后在我使用的目标视图控制器中(例如在 viewDidAppear 中使用)
if(self.view.tag == [@"MySeqgueIdentifier" hash])
{
// Do something here...
}
这很酷,因为您不必创建任何属性等,并且一切都可以在界面生成器中正常工作。
【讨论】: