无法从另一个类访问类实例

时间:2022-01-06 16:01:02

in my AppDelegate I have imported the header of a class I have created and propertied and syntesized an instance of it (in AppDelegate). Now I'm trying to access a method and a variable inside this instance from two other views. I'm relatively new to objective-c, but so far I've learned that if I do this:

在我的AppDelegate中,我已经导入了我创建和属性的类的标题,并将它的实例同步(在AppDelegate中)。现在我试图从另外两个视图中访问此实例中的方法和变量。我对Objective-c比较新,但到目前为止,我已经了解到如果我这样做:

AppDelegate *appdelegate = [AppDelegate new];

I will just get a fresh instance from the class inside AppDelegate, so if I set a variable from one view, I can't access it from the other. I've read that if I would do it this way:

我将从AppDelegate中的类中获取一个新的实例,所以如果我从一个视图设置一个变量,我就无法从另一个视图中访问它。我读过如果我这样做的话:

AppDelegate *ap = (AppDelegate *)[[UIApplication sharedApplication] delegate];

It would allow me to access the existing instance. But it doesn't work.

它允许我访问现有实例。但它不起作用。

Is the way I'm trying to do this totally wrong? Thanks a lot for your help!

我试图做这个完全错误的方式是什么?非常感谢你的帮助!

UPDATE:

When I do this inside AppDelegate:

当我在AppDelegate中执行此操作时:

myClass.string = @"test";
NSLog(@"appDelegate: %@", myClass.string);

I get this:

我明白了:

appDelegate: (null)

UPDATE2:

I wrote @class AppDelegate; underneath the @import lines in the viewController, but still I can't access myClass. A main problem, which may be the cause why this isn't working from the views, is that I can't even access myClass from AppDelegate.

我写了@class AppDelegate;在viewController中的@import行下面,但我仍然无法访问myClass。一个主要问题,可能是因为这不能从视图中工作的原因,我甚至无法从AppDelegate访问myClass。

In AppDelegate.h I wrote:

在AppDelegate.h我写道:

@property (strong, nonatomic) testClass *myClass;

In AppDelegate.m:

#import "testClass.h"
@synthesize myClass;

This should be right, right?

这应该是对的,对吗?

myClass.h

 @property (strong, nonatomic) NSString *string;

myClass.m

@synthesize string;

When I then try to access myClass from appDelegate, I write:

当我尝试从appDelegate访问myClass时,我写道:

self.myClass.string = @"test";
NSLog(@"appDelegate: %@", self.myClass.string);

The result is:

结果是:

appDelegate: (null)

3 个解决方案

#1


2  

I think you have to allocate and initialize the myClass

我认为你必须分配和初始化myClass

Write myClass = [MyClass alloc] init] in AppDelegate.m file

在AppDelegate.m文件中编写myClass = [MyClass alloc] init]

#2


2  

#import "AppDelegate"

and also write

还写

@class AppDelegate;

#3


2  

Unless you haven't shown it, you're not allocating testClass and not assigning it to myClass. Objective-C is not like C++ or Java where you can simply declare a variable of a particular class type and have it instantiated on the stack. Each class you use must be instantiated, whether manually or through InterfaceBuilder. The exception is there are some classes provided by the various frameworks which have a single shared instance. Rather than allocating those classes, you simply ask for the shared instance. However, that's not the case here. It's your own class, so you need to allocate it.

除非您没有显示它,否则您不会分配testClass而不会将其分配给myClass。 Objective-C与C ++或Java不同,您可以简单地声明特定类类型的变量并在堆栈上实例化它。您使用的每个类都必须实例化,无论是手动还是通过InterfaceBuilder。例外是各种框架提供了一些具有单个共享实例的类。您只需要求共享实例,而不是分配这些类。但是,情况并非如此。这是你自己的类,所以你需要分配它。

It would look like:

它看起来像:

myClass = [[testClass alloc] init];

#1


2  

I think you have to allocate and initialize the myClass

我认为你必须分配和初始化myClass

Write myClass = [MyClass alloc] init] in AppDelegate.m file

在AppDelegate.m文件中编写myClass = [MyClass alloc] init]

#2


2  

#import "AppDelegate"

and also write

还写

@class AppDelegate;

#3


2  

Unless you haven't shown it, you're not allocating testClass and not assigning it to myClass. Objective-C is not like C++ or Java where you can simply declare a variable of a particular class type and have it instantiated on the stack. Each class you use must be instantiated, whether manually or through InterfaceBuilder. The exception is there are some classes provided by the various frameworks which have a single shared instance. Rather than allocating those classes, you simply ask for the shared instance. However, that's not the case here. It's your own class, so you need to allocate it.

除非您没有显示它,否则您不会分配testClass而不会将其分配给myClass。 Objective-C与C ++或Java不同,您可以简单地声明特定类类型的变量并在堆栈上实例化它。您使用的每个类都必须实例化,无论是手动还是通过InterfaceBuilder。例外是各种框架提供了一些具有单个共享实例的类。您只需要求共享实例,而不是分配这些类。但是,情况并非如此。这是你自己的类,所以你需要分配它。

It would look like:

它看起来像:

myClass = [[testClass alloc] init];