将 NSString 从 ViewController 传递到 NSObject
您好,我正在尝试将 ViewController 中的 NSString 传递给 NSObject 中的 NSString。 这是我在 NSObject 中使用的代码,用于调用该字符串并将其传递给我的新字符串。
RegisterDeviceViewController *S = [[RegisterDeviceViewController alloc] init];
tempRegCode = S.checkString;
NSLog(@"tempRegCode=%@",tempRegCode);
问题是,一旦按下视图上的按钮,启动我的 NSObject 中的方法,它就会很好地工作,但是没有任何内容传递给 tempRegCode ..这是我的日志。
[会议开始于 2011-05-03 09:27:35 +1200。] 2011-05-03 09:27:36.264 instaCode1.3[1456:207] 嗯嗯,喂我更多饼干!!函数=错误 代码=1 Text=请注册设备 2011-05-03 09:27:36.266 instaCode1.3[1456:207] cookieCode = code1 2011-05-03 09:27:37.983 instaCode1.3[1456:207]alerts.m 按下确定按钮 2011-05-03 09:27:49.539 instaCode1.3[1456:207] 用户注册为“22222-22222-22222-22222” 2011-05-03 09:27:49.540 instaCode1.3[1456:207] tempRegCode=(null)
正如你在最后看到的 'tempRegCode=(null)??
编辑:: 这就是我将变量传递给 checkString 的方式
- (IBAction)submitRegistration:(id)sender{
//NSLog(@"submit Registration button has been pressed");
//add text format here
checkString = regTextField.text;
NSLog(@"Users Registration is '%@'",checkString);
regConnection *Reg= [[regConnection alloc] init];
[Reg startRegConnect];
}
Hi Im trying to pass a NSString thats in a ViewController over to a NSString in an NSObject.
Here is the code that I am using in my NSObject to call the string and pass it to my new string.
RegisterDeviceViewController *S = [[RegisterDeviceViewController alloc] init];
tempRegCode = S.checkString;
NSLog(@"tempRegCode=%@",tempRegCode);
the problem being once the button it pressed on the view that starts the method in my NSObject it works sweet however nothing gets passed to tempRegCode.. here is my Log.
[Session started at 2011-05-03 09:27:35 +1200.]
2011-05-03 09:27:36.264 instaCode1.3[1456:207] yum yum feed me more cookie!! func=ERROR
Code=1
Text=Please Register Device
2011-05-03 09:27:36.266 instaCode1.3[1456:207] cookieCode = code1
2011-05-03 09:27:37.983 instaCode1.3[1456:207] alerts.m OK button pressed
2011-05-03 09:27:49.539 instaCode1.3[1456:207] Users Registration is '22222-22222-22222-22222'
2011-05-03 09:27:49.540 instaCode1.3[1456:207] tempRegCode=(null)
As you can see at the end there 'tempRegCode=(null)??
EDIT::
This is how I passed the variable to checkString
- (IBAction)submitRegistration:(id)sender{
//NSLog(@"submit Registration button has been pressed");
//add text format here
checkString = regTextField.text;
NSLog(@"Users Registration is '%@'",checkString);
regConnection *Reg= [[regConnection alloc] init];
[Reg startRegConnect];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否将属性
checkString
设置为视图控制器的-init
方法中的任何内容?如果不这样做,它将默认为 NULL。类似这样的内容(在 RegisterDeviceViewController.m 文件中):
如果您尝试获取已实例化的 RegisterDeviceViewController 副本中的字符串,则需要对该副本的引用,而不是新分配的副本。例如,如果您的 XIB 文件中有一个 RegisterDeviceViewController 实例,请在 NSObject 子类中使用 IBOutlet 到该实例,然后对该实例调用
-tempRegCode
。Do you set the property
checkString
to anything in the-init
method of your view controller? If you don't, it will default to NULL.Something like this (in your RegisterDeviceViewController.m file):
If you are trying to get a string that is in an already-instanciated copy of RegisterDeviceViewController, you need a reference to that copy, not to a newly allocated one. For example, if you have an instance of RegisterDeviceViewController in your XIB file, use an IBOutlet to this instance in your NSObject subclass then call
-tempRegCode
on that instance.