【问题标题】:Passing data between views not working在视图之间传递数据不起作用
【发布时间】:2013-02-17 02:48:36
【问题描述】:

我知道这个问题被问了大约 100 次,但我就是不知道我做错了什么。

这是我的第二个 iOS 应用。我想通过按下 ViewControllerA 中的按钮来修改 ViewControllerB 中的标签。

  1. 我在 Storyboard 中从我的 Button (ViewControllerA) 到我的 ViewControllerB 制作了一个 Segue,并将其命名为“TestSegue”。
  2. 在我的ViewControllerA.h 中写了#import "ViewControllerB.h
  3. 我在 ViewControllerA.m 中编写了这段代码:

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

if([segue.identifier isEqualToString:@"TestSegue"]){

ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
controller.TestLabel.text = @"TEST!";}}

在我的 ViewControllerB 中,TestLabel 没有任何反应...

谁能看出我做错了什么?

提前致谢

编辑: 现在可以使用 rpledge。

新代码:

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

if([segue.identifier isEqualToString:@"TestSegue"]){

ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
控制器.TestString = @"测试!";}}

【问题讨论】:

    标签: objective-c xcode segue


    【解决方案1】:

    这可能是因为在运行 viewDidLoad 之前没有创建 TextLabel UI 元素,这将在 prepareForSegue 之后发生:

    无论如何,我不喜欢公开展示视图的 UI 元素,我建议您将 NSString* 的 @property 添加到目标视图控制器,然后在 viewDidLoad 中设置 UILabel.text:

    【讨论】:

      【解决方案2】:

      不要将它分配给控件,而是分配给实例变量。

      @property (nonatomic) NSString *var;
      

      然后在 viewDidLoad 中将这个赋值给控件。

      【讨论】:

        猜你喜欢
        • 2014-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-06
        • 1970-01-01
        • 1970-01-01
        • 2014-06-18
        • 1970-01-01
        相关资源
        最近更新 更多