【问题标题】:iOS 5 segue implementationiOS 5 segue 实现
【发布时间】:2011-11-27 13:33:35
【问题描述】:

在两个视图控制器之间实现转场时,如何使用转场对象更改目标视图控制器的属性?文档说这可以在 prepareForSegue: sender: 方法中完成。我试过但没有成功

【问题讨论】:

    标签: storyboard ios5


    【解决方案1】:

    我不知道你是否还需要这个问题的答案,但这是一个如此孤独的帖子,如果我是正确的,这不再属于 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 都是目标视图控制器的合成访问器。希望这会有所帮助!

    【讨论】:

    • 它属于 NDA 但在 3 天内它不会在 10 月 14 日正式发布,但我喜欢你的回答 +1
    • 我试过你说的,但我的destinationViewController 上的属性没有更新。有什么建议吗?
    • 我认为您使用了目标视图控制器的合成访问器(不是私有 ivar),并且在应用程序设置期间已经实例化了视图控制器(即,其中放置了实际视图控制器的情节提要,a它们之间的 segue 都实例化了@应用程序启动)。在这种情况下,它应该可以工作。然后可能是系统不知道您的特定目标。视图控制器类。我的示例中的 ViewController 类是 UIViewController 的子类,在 @implementation 之前我会执行类似“#import "ViewController.h"”的操作。会不会是其中之一?
    【解决方案2】:

    我编写了一个教程,其中提供了代码示例,用于将信息传递到新场景以及使用委托从场景返回信息,因此请查看它应该可以解决您的问题。 iOS 5 Storyboard: How To use Segues, Scenes and Static Content UITableViews

    【讨论】:

    • 我在点击这篇文章之前阅读了这篇博客。谢谢你的博客。
    【解决方案3】:

    我制作了一个关于这个主题的视频。我希望它有所帮助。 http://full.sc/17yKkZF

    【讨论】:

      【解决方案4】:

      我在源视图控制器中使用的是:

      - (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...
      }
      

      这很酷,因为您不必创建任何属性等,并且一切都可以在界面生成器中正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-20
        • 1970-01-01
        • 1970-01-01
        • 2013-01-14
        • 2013-09-20
        • 2012-03-05
        • 2014-10-08
        • 1970-01-01
        相关资源
        最近更新 更多