难以访问另一个视图控制器中的对象
我在名为 ViewController 的视图控制器中设置一个字符串,并尝试在其他地方访问它。这是代码:
ViewController.h
NSString *string;
...
@property (retain) NSString *string;
ViewController.m
@synthesize string;
...
-(void)viewDidLoad {
...
string = @"Test";
}
OtherViewController.m
#import "ViewController.h"
...
-(void)viewDidLoad {
ViewController *vc;
vc = [[ViewController alloc] init];
NSLog(@"String: %@", vc.string);
}
但是,日志显示:String: (null)
。我做错了什么?谢谢。
I'm setting a string in a view controller called ViewController and trying to access it somewhere else. This is the code:
ViewController.h
NSString *string;
...
@property (retain) NSString *string;
ViewController.m
@synthesize string;
...
-(void)viewDidLoad {
...
string = @"Test";
}
OtherViewController.m
#import "ViewController.h"
...
-(void)viewDidLoad {
ViewController *vc;
vc = [[ViewController alloc] init];
NSLog(@"String: %@", vc.string);
}
However, the log is showing: String: (null)
. What am I doing incorrectly? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ViewController
的viewDidLoad
仅在加载view
时调用。view
在需要时延迟加载,例如,当调用vc.view
时。我不确定你想要实现什么,但这对我来说确实像是一种代码味道。
正如 @Fscheidl 指出的那样,您正在创建一个新实例而不是访问现有实例,因此这可能会增加您的问题。我仍然相信您的主要问题是您假设仅通过创建
viewController
来调用viewDidLoad
,但事实并非如此The
viewDidLoad
ofViewController
is only called when theview
is loaded. Theview
is lazily loaded when required e.g. when a call tovc.view
is made.I'm not sure what you are trying to achieve but this certainly seems like a code smell to me.
As @Fscheidl points out you are creating a new instance and not accessing an existing instance so this may add to your problem. I still believe your main issue is that you assume
viewDidLoad
is being called just by creating theviewController
, which is not the case编辑:它不一定需要是 NSObject 类,如果您愿意,您也可以在 viewController 类上执行此操作,只要确保也包含
在您的标题中
编辑结束
----如果您尝试,则 要创建一个可供另一个视图控制器访问的类,为什么不尝试使用 NSObject 而不是视图控制器(考虑到您只需要获取该字符串值),
例如,让我们将该 viewController 类称为“全局”类
,因此在 global.h 中,您将然后
,在 global.m 你把在
此之后,每次您需要访问全局类中包含的“myString”对象时,您可以将其放在
标头:
实现文件中:
就可以了;)
edit : it doesn't necessarily need to be an NSObject class, if you want to, you could also do this on your viewController class, just be sure to also include
on your header
---- end of edit
if you're trying to make a class that's accessible to another view controller, why not try NSObject instead of view controller (considering you only need to take that string value)
for instance, lets call that viewController class "global" class
so at global.h, you put up
and then, at global.m you put up
after this, everytime you need to access the "myString" object that contained in global class, you could put up
at header :
at implementation file :
there you go ;)
您可以通过调用
[[ViewController alloc] init];
创建一个新的ViewController
实例,该实例甚至还没有设置string
。您必须访问ViewController
的确切实例。如果您直接从
ViewController
创建OtherViewController
实例,则可以将以下内容添加到OtherViewController.h
:创建
OtherViewController< /code>,然后您可以设置:
在您的
viewDidLoad:
方法中,您可以按如下方式访问您的字符串:You do create a new instance of
ViewController
by calling[[ViewController alloc] init];
This instance hasn't hadstring
even set. You have to access that exact instance ofViewController
.If you create the instance of
OtherViewController
directly fromViewController
, you can add the following toOtherViewController.h
:When creating the
OtherViewController
, you can then set:In your
viewDidLoad:
method, you can then access your string as follows: