【问题标题】:On storyboards, views and passing data along在故事板上,视图和传递数据
【发布时间】:2011-12-12 22:37:17
【问题描述】:

在我的 Xcode 4.2 故事板中,我有 2 个 UIViewControllersThis one timeIn band camp

  • This one time,我有一个UIButton,上面写着“傻名字”
  • In band camp 中,我有一个带有“标签”的UILabel

考虑到我们正在处理 2 个单独的类 AND 我们在 Xcode 4.2 中使用 storyboards(视图之间的转换是通过 segue 设置的)如何我可以将“愚蠢的名字”从视图控制器This one time 传递给视图控制器In band camp 中的标签吗?”

【问题讨论】:

  • 我没有答案,但你的问题太棒了!

标签: storyboard xcode4.2


【解决方案1】:
  1. 将故事板中 segue 的标识符设置为“AwesomeSegue”
  2. 实现 prepareForSegue 方法
  3. 在方法内部,检查segue的标识符是否匹配“AwesomeSegue” - 如果是,使用destinationViewControllerObject

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([segue.identifier isEqualToString:@"AwesomeSegue"]) {
            InBandCampViewController *ibcVC = [segue destinationViewController];
            ibcVC.yourData = self.someStuff;
        }
    }
    

【讨论】:

    【解决方案2】:

    我必须对我的代码进行一项调整,并且在使用此解决方案时还需要另一种选择。

    在我的例子中,我有一个 导航控制器 在中间打断。 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
    这是因为viewDidLoadSegue之前实现。
    在这里使用viewDidAppear 也无济于事,因为它会在 SecondViewController 呈现到屏幕后加载文本。您可能会看到 UILabel 文本如何从“标签”变为“愚蠢的名称”。

    好吧,如果有人有一些建议,我很高兴听到,因为我也只是一个新手。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 2015-09-15
      • 1970-01-01
      相关资源
      最近更新 更多