难以访问另一个视图控制器中的对象

发布于 2025-01-07 20:20:21 字数 570 浏览 0 评论 0原文

我在名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等风来 2025-01-14 20:20:21

ViewControllerviewDidLoad 仅在加载 view 时调用。 view 在需要时延迟加载,例如,当调用 vc.view 时。

我不确定你想要实现什么,但这对我来说确实像是一种代码味道。

正如 @Fscheidl 指出的那样,您正在创建一个新实例而不是访问现有实例,因此这可能会增加您的问题。我仍然相信您的主要问题是您假设仅通过创建 viewController 来调用 viewDidLoad ,但事实并非如此

The viewDidLoad of ViewController is only called when the view is loaded. The view is lazily loaded when required e.g. when a call to vc.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 the viewController, which is not the case

岁月苍老的讽刺 2025-01-14 20:20:21

编辑:它不一定需要是 NSObject 类,如果您愿意,您也可以在 viewController 类上执行此操作,只要确保也包含

-(id)init

在您的标题中

编辑结束

----如果您尝试,则 要创建一个可供另一个视图控制器访问的类,为什么不尝试使用 NSObject 而不是视图控制器(考虑到您只需要获取该字符串值),

例如,让我们将该 viewController 类称为“全局”类

,因此在 global.h 中,您将然后

#import <Foundation/Foundation.h>

@interface GlobalVar : NSObject

@property (nonatomic, strong) NSString *myString;


-(id)init;

@end

,在 global.m 你把在

#import "GlobalVar.h"

@implementation GlobalVar

@synthesize myString;

-(id)init
{
    self = [super init];
    if(self)
    {
        myString = [[NSString alloc]initWithFormat:@"the String"];
    }

    return self;
}



@end

此之后,每次您需要访问全局类中包含的“myString”对象时,您可以将其放在

标头:

#import "GlobalVar.h"
...
...
@property (nonatomic, strong) GlobalVar *globalVar;

实现文件中:

@synthesize globalVar;

...
...

self.globalVar = [[GlobalVar alloc]init];
NSString *theString = globalVar.myString;
NSLog(@"content of my string is : %@",theString);

就可以了;)

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

-(id)init

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

#import <Foundation/Foundation.h>

@interface GlobalVar : NSObject

@property (nonatomic, strong) NSString *myString;


-(id)init;

@end

and then, at global.m you put up

#import "GlobalVar.h"

@implementation GlobalVar

@synthesize myString;

-(id)init
{
    self = [super init];
    if(self)
    {
        myString = [[NSString alloc]initWithFormat:@"the String"];
    }

    return self;
}



@end

after this, everytime you need to access the "myString" object that contained in global class, you could put up

at header :

#import "GlobalVar.h"
...
...
@property (nonatomic, strong) GlobalVar *globalVar;

at implementation file :

@synthesize globalVar;

...
...

self.globalVar = [[GlobalVar alloc]init];
NSString *theString = globalVar.myString;
NSLog(@"content of my string is : %@",theString);

there you go ;)

遗心遗梦遗幸福 2025-01-14 20:20:21

您可以通过调用 [[ViewController alloc] init]; 创建一个新的 ViewController 实例,该实例甚至还没有设置 string 。您必须访问 ViewController确切实例。

如果您直接从 ViewController 创建 OtherViewController 实例,则可以将以下内容添加到 OtherViewController.h

#import "ViewController.h"
@property (nonatomic, retain) ViewController *previousViewController

创建 OtherViewController< /code>,然后您可以设置:

//alloc and init instance of OtherViewController
myOtherViewController.previousViewController = self;

在您的 viewDidLoad: 方法中,您可以按如下方式访问您的字符串:

NSLog(@"String: %@", previousViewController.string);

You do create a new instance of ViewController by calling [[ViewController alloc] init]; This instance hasn't had string even set. You have to access that exact instance of ViewController.

If you create the instance of OtherViewController directly from ViewController, you can add the following to OtherViewController.h:

#import "ViewController.h"
@property (nonatomic, retain) ViewController *previousViewController

When creating the OtherViewController, you can then set:

//alloc and init instance of OtherViewController
myOtherViewController.previousViewController = self;

In your viewDidLoad: method, you can then access your string as follows:

NSLog(@"String: %@", previousViewController.string);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文