【问题标题】:Can´t pass a NSString from one view to an other view不能将 NSString 从一个视图传递到另一个视图
【发布时间】:2010-01-10 23:21:13
【问题描述】:

我尝试将 NSString 从一个视图传递到另一个视图,但我不起作用。我在 SecondViewController 中设置了一个 NSString 作为属性

@property (assign) NSString * wert1;

当我在 FirstViewController 上按下按钮加载 SecondViewController 时,我尝试传递 NSString:

SecondViewController *Second = [[SpinViewController alloc] initWithNibName:nil bundle:nil];
Second.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"black.png"]];
[Second setWert1:texteingabe1]; //HERE <<<<
Second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:Second animated:YES];
[Second release];

在 SecondViewController 中我做了这个:

NSLog(@"%@",wert1);

但是 NSLog 只是说:(null)。为什么?

感谢您的帮助,对不起我的英语不好。

【问题讨论】:

    标签: iphone objective-c xcode


    【解决方案1】:

    你在哪里调用 NSLog 声明?如果它在loadViewviewDidLoad 中,它将不起作用,因为在您的示例中调用setWert1: 之前调用了这些方法。

    另外,请确保在调用 NSLog 之前没有将其设置为 nil,当然,请确保 texteingabe1 在传入时实际上不是 nil。

    【讨论】:

      【解决方案2】:

      如果您设置原始字符串文字,它会起作用吗?即 [第二个 setWert1:@"Foo"]?在没有看到更多代码的情况下,在第二个视图中,字符串值为 NULL 的最可能原因是您为“texteingabe1”传递了一个 NULL 字符串。

      【讨论】:

        【解决方案3】:

        您需要复制或保留第二个控制器中的字符串。使用@property 关键字“retain”而不是“assign”。

        Assign 只是将字符串分配给您在 Second 中的实例成员...然后当字符串在 First 控制器中释放时,它也会从 Second 中消失。

        【讨论】:

          【解决方案4】:

          找到解决方案。我刚刚将背景代码移到了 setWert1-code 下,现在它可以工作了:)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-05-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-10-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多