简体   繁体   English

如何从另一个类设置NSString *属性?

[英]How to set NSString* property from another class?

Please look to the following code. 请查看以下代码。

FirstViewController.h: FirstViewController.h:

@interface FirstViewController : UIViewController {
    NSString* inputData;
}

@property (nonatomic, copy/retain) NSString* inputData;

FirstViewController.m: FirstViewController.m:

@synthesize inputData;

SecondViewController.m: SecondViewController.m:

- (IBAction)buttonSaveDown {
    [firstViewController setInputData:@"Text"];
    firstViewController.inputData = @"Text";
    firstViewController.inputData = [[NSString stringWithString:@"Text"] retain];
}

Add breakpoints after every line with inputData. 使用inputData在每一行之后添加断点。 Check firstViewController.inputData - it's '(null)'! 检查firstViewController.inputData - 它是'(null)'! Why? 为什么?

Printing description of inputData:
(null)

Also I try 'Edit value' in the debugger. 我也尝试在调试器中“编辑值”。 After setting value the result is '(null)' too. 设置值后,结果也是'(null)'。

There's no any warnings on the build. 构建中没有任何警告。 Please help anybody. 请帮助任何人。 Thanks a lot! 非常感谢!

To moderators: Sorry, but someone deleted the same previous question when I asked to delete all comments on it. 主持人:很抱歉,当有人要求删除所有评论时,有人删除了上一个相同的问题。

Your firstViewController variable is not set to anything. 您的firstViewController变量未设置为任何内容。 You need to set it to a valid instance of FirstViewController before you can do anything useful with it. 您需要将它设置为FirstViewController的有效实例,然后才能对其执行任何有用的操作。

Just use 只是用

@property (nonatomic, retain) NSString* inputData;

Then these each should work: 然后这些应该工作:

[firstViewController setInputData:@"Text"];
firstViewController.inputData = @"Text";
firstViewController.inputData = [NSString stringWithString:@"Text"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM